简体   繁体   English

我的控制台中的错误=>(core.js:6456 错误类型错误:无法读取未定义的属性'doc')

[英]Error in my Console=> (core.js:6456 ERROR TypeError: Cannot read property 'doc' of undefined)

I am trying to over come this problem and I am can't=> My Console Error as Following:我正在尝试解决这个问题,但我不能=> 我的控制台错误如下:


core.js:6456 ERROR TypeError: Cannot read property 'doc' of undefined at 
home.component.ts:22 at Array.map (<anonymous>) at 
SafeSubscriber._next (home.component.ts:18) at 
SafeSubscriber.__tryOrUnsub (Subscriber.js:183) at
SafeSubscriber.next (Subscriber.js:122) at Subscriber._next (Subscriber.js:72) at 
Subscriber.next (Subscriber.js:49) at Notification.observe (Notification.js:20) at 
AsyncAction.dispatch (observeOn.js:25) at 
angular-fire.js:27

. .

import { Component, OnDestroy, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { Good } from 'src/app/interfaces/good.interface';
import { GoodsService } from 'src/app/services/goods.service';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit, OnDestroy {
goods :Array<Good> =[];
goodsObservable!: Subscription;
  constructor(private gs : GoodsService) { }

  ngOnInit (): void {
    this.goodsObservable = this.gs.getAllGoods().subscribe(data => {
       this.goods = data.map((element:any)=>{
         console.log(data);
        return {
          
          **id: element.payload.doc.id,
          ...element.payload.doc.data()**
        }
      })
    })
  }

  ngOnDestroy (){
   this.goodsObservable.unsubscribe();
  }
addToCart(id: any) {
  console.log("added", id)
}
}

You should verify if data is an array and that all elements inside data have payload property.您应该验证data是否是一个数组,并且 data 中的所有元素都具有payload属性。 Otherwise, you can use conditional properties like this否则,您可以使用这样的条件属性

   return {
      
      **id: element?.payload?.doc?.id,
      ...element?.payload?.doc?.data()**
    }

It avoids to get the error in the console, but still id will remain null or undefined它避免在控制台中出现错误,但id仍将保持null或未定义

暂无
暂无

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

相关问题 Angular - core.js:6456 错误类型错误:无法读取 null 图片上传中未定义的属性“名称” - Angular - core.js:6456 ERROR TypeError: Cannot read property 'name' of undefined in null image upload core.js:4002 错误类型错误:无法读取未定义的属性 *** - core.js:4002 ERROR TypeError: Cannot read property *** of undefined 面对错误core.js:15723错误TypeError:无法读取Angular 7中未定义的属性“ toLowerCase” - Facing error core.js:15723 ERROR TypeError: Cannot read property 'toLowerCase' of undefined In Angular 7 core.js:6228 错误错误:未捕获(承诺中):TypeError:无法读取未定义的属性“id” - core.js:6228 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'id' of undefined core.js:1673 错误错误:未捕获(承诺中):TypeError:无法读取未定义的属性“路由” - core.js:1673 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'routes' of undefined core.js:9110 错误类型错误:无法读取未定义的属性“post” - core.js:9110 ERROR TypeError: Cannot read property 'post' of undefined core.js:15723错误TypeError:无法读取未定义的属性“ OBJECT” - core.js:15723 ERROR TypeError: Cannot read property 'OBJECT' of undefined core.js:4442 错误类型错误:无法读取未定义的属性“修剪” - core.js:4442 ERROR TypeError: Cannot read property 'trim' of undefined core.js:5873 错误类型错误:无法读取未定义的属性“文件名” - core.js:5873 ERROR TypeError: Cannot read property 'filename' of undefined core.js:5980 错误类型错误:无法读取未定义的属性“名称”(角度) - core.js:5980 ERROR TypeError: Cannot read property 'Name' of undefined (angular)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM