Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
652 views
in Technique[技术] by (71.8m points)

angular - ERROR TypeError: Cannot read property 'productId' of undefined

I've gotten somewhat stuck on this and I'm hoping someone could help.

I'm getting the error ERROR TypeError: Cannot read property 'productId' of undefined. The peculiar thing is that the Observable I'm using will appear fully populated (including productId) when using {{ product | json }}. In fact {{ product.productId }} displays on the page as well. The only issue is the error appears in the console and I'm unsure how to handle it.

My code is below

HTML

<p>detail works!</p>

<div *ngIf="product===undefined && product===null;else detailInfo">
    this
</div>
<ng-template #detailInfo>
    
{{ product.id }}
{{ product.pTitle }}
{{ product.pLDescr }}
{{ product | json }}
</ng-template>

Component

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { DataService } from '../data.service';
import { iPrd } from '../Interfaces/iPrd';

@Component({
  selector: 'app-detail',
  templateUrl: './detail.component.html',
  styleUrls: ['./detail.component.css']
})
export class DetailComponent implements OnInit {

  constructor(
    private route: ActivatedRoute, 
    private data: DataService 
  ) { }

  public product!: iPrd;

  getProdctDetail(): void {
    let id = Number(this.route.snapshot.paramMap.get('Id')); 
    this.data.getProductDetail(id.toString()).subscribe(data => { this.product = data; console.log(data) });
  } 

  ngOnInit(): void {
    this.getProdctDetail();
  }

}

Service

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { iPrd } from './Interfaces/iPrd';
import { Observable, of } from 'rxjs';
import { product } from './Interfaces/product';


@Injectable({
  providedIn: 'root'
})


export class DataService {

  
  public rootServer = 'https://localhost'
  public imgDir = '/imgDir'
  
  public getProducts(): Observable<prd[]>{
    return this.getDataArr('/api/myPrd/')
  }; 
  
  public getProductDetail(productId:string): Observable<iPrd>{
    return this.getData('/api/myPrd/' + productId)
  }

  public getDataArr(url:string): Observable<any[]>{
    return this.http.get<any>( this.rootServer + url)
  };

  public getData(url:string): Observable<any>{
    return this.http.get<any>( this.rootServer + url)
  }


  constructor(private http:HttpClient) { }
}

interface

export interface iPrd {
    id: number; 
    pTitle: string; 
    pSDescr: string;
    pLDescr: string; 
    pTypId: number; 
    pImgUrl: string; 
    pDetailUrl?: string; 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this in your html

<p>detail works!</p>

<div *ngIf="product">
        {{ product?.id }}
        {{ product?.pTitle }}
        {{ product?.pLDescr }}
        {{ product | json }}
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...