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

时间:2017-03-02 11:34:57

标签: json string angular typescript observable

您好我是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)
            );
    }
}

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());
    }
}

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>

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

Bicycles
|
---bmx
    |
    ---stock = 1234;

onSubmit()之后的数据库

Bicycles
|
--- bmx
     |
     ---stock
         |
         --- _isScalar = false

我正在使用Firebase。

我知道它可以使用字符串,因为我用这样测试它:

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

这对我的数据库来说是什么

Bicycles
|
---bmx
    |
    ---stock = 33333

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

基本上我想改变&#34; stock&#34;在我的数据库中,但每次按下“更改数据库”按钮时,按固定的数量,例如,每次按下它时减去1。

2 个答案:

答案 0 :(得分:4)

订阅observable以获取结果,并在收到值时致电onSubmit

currentStock = this.httpService.getDataBmx2()
.subscribe(val => this.onSubmit(val));

答案 1 :(得分:0)

对象具有toString方法,您可以实现该方法以显示对象的值,或将其转换为JSON.stringify()这样的字符串

this.httpService.sendData({ stock: asnNumm })
        .subscribe(
        data => console.log(JSON.stringify(data)),
        error => console.log(error)
        );

您必须映射到响应对象以获取数据,以获取数据作为文本,您可以查询响应对象

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


export class BmxComponent {
    currentStock: string;

   this.httpService.getDataBmx2().subscribe(s => this.currentStock = s); //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