亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

實時更新單個火庫文檔

實時更新單個火庫文檔

慕碼人2483693 2022-09-29 16:39:42
有很多消防商店集合獲取實時更新的文檔和示例。但是,對于那些希望單個文檔進行實時更新的人來說,幾乎沒有什么。我想在一個頁面上有一個文檔(一個),只有頁面將被查看和操作,并且對文檔的任何更改,都會有實時更新。itemitem這是我的組件,想要用做一些事情:itemimport { Component, OnInit } from '@angular/core';import { ItemsService } from '../shared/items.service';import { ActivatedRoute, Router } from '@angular/router';@Component({  selector: 'app-view-item',  templateUrl: './view-item.component.html',  styleUrls: ['./view-item.component.css']})export class ViewItem implements OnInit {  item;  private sub: any;  constructor(    // Service used for Firebase calls    private itemsService: ItemsService,    private route: ActivatedRoute,    private router: Router  ) {}  ngOnInit() {    // Item retrieved from */item/:id url    this.sub = this.route.params.subscribe(params => {      this.getItem(params['id']);    });  }  getItem = (id) => {    this.itemsService.getItem(id).subscribe(res => {      console.log(res);      this.item = res;      console.log(this.item);    });  }我得到的日志是 。在控制臺中調用將返回相同的結果。我不確定如何繼續,并希望得到任何指導。在控制臺中登錄將返回一個拜占庭對象。也許這就是我訪問的方式,但如果是這樣,為什么它沒有保存,我如何訪問 的值?console.log(this.item)undefinedthis.itemresitemthis.itemitem
查看完整描述

1 回答

?
智慧大石

TA貢獻1946條經驗 獲得超3個贊


snapshotChanges返回可觀察的操作,而不是實際值。應使用 提取值:action.payload.doc.data()


因此,您的代碼應類似于以下示例。


getItem(id) {

  return this.firestore.collection('items').doc(id).snapshotChanges()

    .pipe(

      map(actions => actions.map(a => {

        const data = a.payload.doc.data();

        const id = a.payload.doc.id;

        return { id, ...data };

      })

    );

}

或者您可以使用 .valueChangesdoc


getItem(id) {

  return this.firestore.collection('items').doc(id).valueChanges();

}


查看完整回答
反對 回復 2022-09-29
  • 1 回答
  • 0 關注
  • 91 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號