错误类型错误:无法读取未定义的属性“doc”

时间:2021-04-02 06:25:50

标签: angular firebase angular8

我是 angular 新手,我真的需要帮助并提前致谢 发生的文本错误是:错误类型错误:无法读取未定义的属性“doc” 应用程序

  getTask() {
    this.ticketService.getTask(this.id).valueChanges({ idField: 'id' }).subscribe(task => {
      this.depositTasks = task
      task.map(items => {
        console.log(items);
        for (let i = 0; this.depositTasks.length > i; i++) {
          const item = items.payload.doc.data()
          item['$uid'] = items.payload.doc['id'];
          this.Task.push(item as Tasks)
        }
      })
    })
  }

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试将您的代码更新为:

getTask() {
    this.ticketService
        .getTask(this.id)
        .valueChanges({ idField: 'id' })
        .subscribe(task => {
            task.map(items => {
                console.log(items);
                const item = items;
                item['$uid'] = items['id'];
                this.Task.push(item as Tasks);
            })
        })
}

答案 1 :(得分:0)

您正在尝试访问不属于对象的变量,因此 undefined 错误。

请尝试修改您的代码,如下所示,

getTask() {
    this.ticketService.getTask(this.id).valueChanges({ idField: 'id' }).subscribe(task => {
      this.depositTasks = task;
      this.Task = [ ];
      task.forEach(items => {
        console.log(items);
        let item: any = {};
        item['$uid'] = items.id;
        this.Task.push(item as Tasks);
      });
  });

快乐编码.. :)