简体   繁体   English

对单个 Firestore 文档进行实时更新

[英]Have realtime updates for a single Firestore document

There is a lot of documentation and examples of firestore collections getting realtime updates.有很多关于 Firestore collections 获取实时更新的文档和示例。 However, there is very little for those who wish to have a single document have real time updates.但是,对于那些希望单个文档具有实时更新的人来说,几乎没有。 I want to have a single document (an item ), on a page where only the item will be viewed and manipulated and any changes to document, will have realtime updating.我想在一个页面上有一个文档(一个item ),只有该item将被查看和操作,并且对文档的任何更改都将实时更新。

Here is my component that wants to do stuff with the item :这是我想要对item做一些事情的组件:

import { 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);
    });
  }

And the service it uses for calls:它用于调用的服务:

import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/firestore';

@Injectable({
  providedIn: 'root'
})
export class ItemsService {
  constructor(
    private firestore: AngularFirestore
  )

  getItem(id) {
    return this.firestore.collection('items').doc(id).snapshotChanges();
  }
}

The log I get for console.log(this.item) is undefined .我为console.log(this.item)获得的日志是undefined Calling this.item in the console returns the same.在控制台中调用this.item会返回相同的结果。 I am unsure of how to proceed and would appreciate any guidance.我不确定如何进行,并希望得到任何指导。 Logging res in the console returns a byzantine object.在控制台中记录res返回一个拜占庭 object。 Perhaps that's how I access the item , but if so, why is it not saved in this.item and how do I access the item 's values?也许这就是我访问item的方式,但如果是这样,为什么它没有保存在this.item中以及如何访问item的值?

snapshotChanges returns an observable of actions , not the actual value. snapshotChanges返回一个可观察的动作而不是实际值。 You should extract the value with action.payload.doc.data() :您应该使用action.payload.doc.data()提取值:

So your code should look like the following example.因此,您的代码应类似于以下示例。

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

Or you can use valueChanges of doc .或者您可以使用docvalueChanges

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM