Angular Firestore - 获取文档数据并分配给变量

标签 angular typescript firebase google-cloud-firestore

我正在尝试将从 firestore 文档收集的数据分配给一个在构造函数之前初始化为 Observable 类型的变量。

我通过将动态 invoiceId 字符串传递给 .doc() 搜索来从集合中获取数据,并且我可以将数据分配给局部变量(如下所示),但是在尝试将其分配给此变量时.invoice,我收到以下错误:

Uncaught (in promise): TypeError: Cannot set property 'invoice' of undefined

-

组件:

import { Component, OnInit, Input } from '@angular/core';

import { ActivatedRoute } from '@angular/router';

import { Observable } from 'rxjs/Observable';

import { AngularFireDatabase } from 'angularfire2/database';

import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';

import { AuthService } from '../../services/auth.service';

import { Invoice } from '../invoiceModel';

@Component({
  selector: 'app-view-invoice',
  templateUrl: './view-invoice.component.html',
  styleUrls: ['./view-invoice.component.scss']
})

export class ViewInvoiceComponent implements OnInit {

  userId: string;

  invoiceId: any;

  invoicesCollection: AngularFirestoreCollection<Invoice>;
  invoices: Observable<Invoice[]>;

  invoice: Observable<Invoice>;

  constructor(private authService: AuthService, private db: AngularFirestore, private route: ActivatedRoute) {
      this.userId = this.authService.user.uid;

      this.route.params.subscribe(params => {
        this.invoiceId = params.id;
      })

      this.invoicesCollection = this.db.collection('/invoices');

      this.invoices = this.invoicesCollection.snapshotChanges().map(changes => {
          return changes.map(a => {
            const data = a.payload.doc.data() as Invoice;
            data.id = a.payload.doc.id;
            return data;
          })
      })
  }

  ngOnInit() {
    this.getInvoice();
  }

  getInvoice() {
    var docref = this.db.collection('/users').doc(this.authService.user.uid).collection('/invoices').doc(this.invoiceId);
    docref.ref.get()
        .then(function(doc) {
            if (doc.exists) {
                var invoice = doc.data(); <------WORKS
                // this.invoice = doc.data(); <------DOESN'T WORK
                console.log('Invoice data: ', doc.data());
            } else {
                console.error('No matching invoice found');
            }
    })
  }

}

最佳答案

我也在和同样的事情作斗争。这让我发疯!我是新手,但我似乎已经通过更改一行代码解决了您的问题:

.then(function(doc) {   //changed from
.then((doc) => {        //changed to (removed the function)

我什至不明白这样做的后果,但范围现在正在努力分配变量的值。

关于Angular Firestore - 获取文档数据并分配给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49067294/

相关文章:

Angular 2 Http 客户端在调用时不执行任何操作

javascript - Angular 2 : Access Cookie after Creation

firebase - 使用 .info/connected 测试 firebase 连接

javascript - Firebase 参数太多。运行 firebase 帮助模拟器 :start for usage instructions

firebase - 如何在 Flutter 中从 Firestore 存储和检索颜色

Angular - this.function 不是一个函数

angular - 如何制作可滚动的垫列表

knockout.js - 在 Typescript 中扩展泛型参数

node.js - Angular 生产错误 : Property does not exist on type 'object'

typescript - 从 Typescript 到 Javascript => required 未在 chrome 浏览器中定义