繁体   English   中英

错误 TypeError:无法读取未定义的属性(读取“_id”)

[英]ERROR TypeError: Cannot read properties of undefined (reading '_id')

我正在尝试在 angular 上显示数据条目(具有唯一 ID),但我收到错误

错误 TypeError:无法读取未定义的属性(读取“_id”)

针对特定 id 的 get 请求的服务是:

getOneDAFacForm(id: string) {
    return this.http.get<any>("http://localhost:3000/DAFacility"+id)
    .pipe(map((res:any)=>{
      return res;
    }))
  }

组件的路径如下:

{ path: 'daform-fac-view-full/:facviewid', component: DaformFacViewFullComponent},

页面组件的 typescript:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { DAFormFac } from 'src/app/models/daformfac.interface';
import { DaformfacserviceService } from 'src/app/service/daformfacservice.service';

@Component({
  selector: 'app-daform-fac-view-full',
  templateUrl: './daform-fac-view-full.component.html',
  styleUrls: ['./daform-fac-view-full.component.css'],
})
export class DaformFacViewFullComponent implements OnInit {
  daformfac: DAFormFac[] = [];

  formID: string;
  getparamformid: any;
  daformfacs: any;

  constructor(
    private daformfacservice: DaformfacserviceService,
    private route: ActivatedRoute
  ) {}

  ngOnInit(): void {
    console.log(
      this.route.snapshot.paramMap.get('facviewid'),
      ' : ID of report'
    );
    this.getparamformid = this.route.snapshot.paramMap.get('facviewid');
    this.daformfacservice.getOneDAFacForm(this.getparamformid).subscribe((daFormFac: DAFormFac[]) => {
        this.daformfacs = daFormFac;
        console.log(daFormFac, 'response of form');
      });
  }
}

在 html 组件中,我试图显示存储的信息,如:

Report ID : {{daformfacs._id}} 

我对此有什么不妥吗,我们将不胜感激。

愚蠢的错误:缺少“/”

getOneDAFacForm(id: string) {
    return this.http.get<any>("http://localhost:3000/DAFacility/"+id)
    .pipe(map((res:any)=>{
      return res;
    }))
  }

错误是说 daformfacs 未定义。 所以你不能访问 daformfacs 上的任何属性,因为它是未定义的。

确保在您尝试使用它的地方导入/定义它。 或者你打错了,而是 daformfac._id、daFormFac._id 或类似的东西。

暂无
暂无

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

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