簡體   English   中英

如何使用Angular 5更新Firebase數據庫的值

[英]How to update value to firebase database with Angular 5

我想做一個upvote功能。 我有一些帖子存儲在Firebase數據庫中。 我想讓人們贊成(或反對)職位。

這是我的ctrl:

upvote(advice) {
    advice.votes = advice.votes + 1;
    return this.httpService.updateData(advice)
    .subscribe((res:Response) => {
        console.log('in subscribe', res);
    });
}

在我的服務中:

updateData (data:any): Observable<any> {

    let datas = JSON.stringify(data);
    const headers = { 'Authorization': 'secret' };

    return this.http.put(this.url + 'data.json', datas, headers)
        .catch((e) => {
            console.log(e);
        })
        .finally(() => {
            console.log('finally');
        })
}

當我在控制器內部的subscription方法中檢查響應時,可以看到更新的值,但是數據庫現在已被破壞。

之前:

錯誤前的屏幕截圖

之后:

在此處輸入圖片說明

如您所見,之后沒有更多的實際對象,它將替換所有內容。 我想我必須更改網址或檢查帖子ID,但是我不知道該怎么做。 我在firebase文檔中什么都沒找到。

@Luke答案后編輯

我這樣更新了代碼:

在我的ctrl中:

upvote(advice) {
    advice.votes = advice.votes + 1;
    return this.httpService.updateData(advice)
    .subscribe((res:Response) => {
        console.log(res);
        return res;
    })
}

並在服務中:

getId() {
    this.db.list('data').snapshotChanges().map(actions => {
        return actions.map(action => ({ key: action.key, ...action.payload.val() }));
    }).subscribe(items => {
        return items.map(item => item.key);
    });
}

updateData (data:any): Observable<any> {

    let updateUrl = this.url + '-p1FFxUqY6u1_AZ3ER4eVUt';

    let datas = JSON.stringify(data);
    const headers = { 'Authorization': 'secret' };

    return this.http.put(updateUrl + 'data.json', datas, headers)
        .catch((e) => {
            console.log(e);
        })
        .finally(() => {
            console.log('finally');
        })
}

我可以在日志和正面看到投票計數器更新,但是在數據庫中看不到。 當我刷新頁面時,投票恢復為0。我想我只是忘記了一些。

如果要將新項目添加到某個位置,則應使用POST而不是PUT

this.http.post(this.url + 'data.json', datas, headers)...

如果要僅提供部分數據來更新現有項目,則應使用PATCH而不是PUT 如果您的HTTP客戶端不支持發送PATCH ,則可以使用GET並在X-HTTP-Method-Override標頭中指定PATCH

有關所有這些的全部詳細信息,請閱讀Firebase文檔,以使用REST API保存數據

要更新數據,您需要知道該特定項目的密鑰才能放入網址; 例如:your-firebase-url /.../ L3-bvgdsggdgde。 像這樣

updateData (data:any): Observable<any> {

        updateUrl = this.url + 'L3-bvgdsggdgde';

        let datas = JSON.stringify(data);
        const headers = { 'Authorization': 'secret' };

        return this.http.put(updateUrl + 'data.json', datas, headers)
            .catch((e) => {
                console.log(e);
            })
            .finally(() => {
                console.log('finally');
            })
    }

那么,如何從Firebase獲取密鑰並重用它呢? 從Firebase獲取數據時,遵循此Firebase文檔

constructor(afDb: AngularFireDatabase) {
  afDb.list('items').snapshotChanges().map(actions => {
    return actions.map(action => ({ key: action.key, ...action.payload.val() }));
  }).subscribe(items => {
    return items.map(item => item.key);
  });
}

您的對象將具有一個關鍵屬性,可在您的upvote / update方法中重復使用它。 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM