Home > Mobile >  ERROR TypeError: Cannot read properties of undefined (reading '_id')
ERROR TypeError: Cannot read properties of undefined (reading '_id')

Time:02-24

I am trying to display a data entry (with a unique id) on angular, but im receiving the error

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

the service for the get request for the particular id is:

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

The path for the component is as follows:

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

the typescript for the page component:

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

and in the html component, i am trying to display the info stored like:

Report ID : {{daformfacs._id}} 

Is there anything im doing wrong with this any assistance would be appreciated.

CodePudding user response:

Silly error: missing the "/"

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

CodePudding user response:

The error is saying daformfacs is undefined. So you can't access any property on daformfacs because is undefined.

Make sure you have it imported/defined where you try to use it. Or you my have a typo and it's instead daformfac._id, daFormFac._id or something similar.

  • Related