如何在打字稿中将存储在 Observable<any> 中的值转换为字符串?

que*_*ons 5 string json observable typescript angular

嗨,我是 Angular 和 TypeScript 的新手。我需要Observable一个字符串格式的值,如何做到这一点?

BmxComponent 文件

export class BmxComponent {
    asyncString = this.httpService.getDataBmx();
    currentStock = this.httpService.getDataBmx2(); //this is what I want to covert to a string so I can pass it to onSubmit()

    onSubmit() {
        const asnNumm = this.currentStock; // passing it here changes my database, see below
        this.httpService.sendData({ stock: asnNumm })
            .subscribe(
            data => console.log(data),
            error => console.log(error)
            );
    }
}
Run Code Online (Sandbox Code Playgroud)

HttpService 文件

export class HttpService {

    constructor(private http: Http) {}

    getDataBmx() {
        return this.http.get('https://the-bicycle-shop.firebaseio.com/products/Bicycles/bmx/stock.json')
            .map((response: Response) => response.json());
    }

    getDataBmx2() {
        return (this.http.get('https://the-bicycle-shop.firebaseio.com/products/Bicycles/bmx/stock.json'));
    }   

    sendData(newStock: any) {
        const body = JSON.stringify(newStock);
        const headers = new Headers();
        headers.append('Content-Type', 'application/json');
        return this.http.patch('https://the-bicycle-shop.firebaseio.com/products/Bicycles/bmx.json', body, {
            headers: headers
        })
            .map((data: Response) => data.json())
            .catch(this.handleError);
    }

    private handleError(error: any) {
        console.log(error);
        return Observable.throw(error.json());
    }
}
Run Code Online (Sandbox Code Playgroud)

html文件

<p>{{asyncString | async}}</p> // displays 1234 which is the correct value
<p>{{asyncString}}</p> // displays [object Object]
<p>{{currentStock}}</p> // displays [object Object]
<button class="btn btn-success" (click)="onSubmit()">Change Database</button>
Run Code Online (Sandbox Code Playgroud)

我在 onSubmit() 之前的数据库(当我单击“更改数据库”按钮时使用)

Bicycles
|
---bmx
    |
    ---stock = 1234;
Run Code Online (Sandbox Code Playgroud)

onSubmit() 之后我的数据库

Bicycles
|
--- bmx
     |
     ---stock
         |
         --- _isScalar = false
Run Code Online (Sandbox Code Playgroud)

我正在为此使用 Firebase。

我知道它可以与字符串一起使用,因为我是这样测试的:

    onSubmit() {
        const asnNumm = "33333" //the change to test it
        this.httpService.sendData({ stock: asnNumm })
            .subscribe(
            data => console.log(data),
            error => console.log(error)
            );
    }
Run Code Online (Sandbox Code Playgroud)

这对我的数据库执行此操作

Bicycles
|
---bmx
    |
    ---stock = 33333
Run Code Online (Sandbox Code Playgroud)

我知道这currentStock将保存当前存储在我的数据库中的相同值,因此它没有区别,但是一旦将其转换为字符串,我想更改它。

基本上,我想更改数据库中的“库存”,但是每次按下更改数据库按钮时更改固定数量,例如,每次按下时减 1。

Gün*_*uer 6

订阅 observable 获取结果并onSubmit在收到值时调用:

currentStock = this.httpService.getDataBmx2()
.subscribe(val => this.onSubmit(val));
Run Code Online (Sandbox Code Playgroud)