简体   繁体   English

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

[英]ERROR TypeError: Cannot read property 'first_name' of undefined

i get this error When i try to render the list to the view from a backend service, am able to get the response data and log it to the console but it wont render on the view.我收到此错误当我尝试从后端服务将列表呈现到视图时,能够获取响应数据并将其记录到控制台,但它不会呈现在视图上。

enter code here在此处输入代码

 import { Component, OnInit, Input } from '@angular/core'; import { StudentsService } from '../services/students.service'; import { Student } from '../interfaces/Student'; @Component({ selector: 'app-student', templateUrl: './student.component.html', styleUrls: ['./student.component.css'], }) export class StudentComponent implements OnInit { @Input() student: Student; students: Student[]; constructor(private studentService: StudentsService) {} ngOnInit() { this.studentService.getStudents().subscribe((students) => { (data) => { this.students = data.json(); Array.of(this.students); }; console.log(students); }); } }

 export interface Student { id: number; first_name: string; last_name: string; date_of_birth: Date; }

 <div class="container mt-5"> <ul *ngFor="let student of students"> <li>{{student.first_name}}</li> </ul> </div>

   (data) => {
        this.students = data.json();
        Array.of(this.students);
      };

it shouldn't be a function.它不应该是 function。 change it to something like this:把它改成这样:

import { Component, OnInit, Input } from '@angular/core';
import { StudentsService } from '../services/students.service';
import { Student } from '../interfaces/Student';

@Component({
  selector: 'app-student',
  templateUrl: './student.component.html',
  styleUrls: ['./student.component.css'],
})
export class StudentComponent implements OnInit {
  @Input() student: Student;
  students: Student[];
  constructor(private studentService: StudentsService) {}

  ngOnInit() {
          this.studentService.getStudents().subscribe((students) => {
      this.students = students; // students is response from server. if it is not Student[] you should map it to correct type.
    });
  }
}

暂无
暂无

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

相关问题 渲染错误:“TypeError:无法读取未定义的属性‘first_name’” - Error in render: "TypeError: Cannot read property 'first_name' of undefined" NodeJS :: TypeError:无法读取未定义的属性“first_name” - NodeJS :: TypeError: Cannot read property 'first_name' of undefined 节点JS | TypeError:无法读取未定义的属性“ first_name” - Node JS | TypeError: Cannot read property 'first_name' of undefined 无法读取未定义的属性“first_name” - Cannot read property 'first_name' of undefined 云函数错误:函数返回未定义、预期的承诺或值,以及类型错误:无法读取未定义的属性“First_Name” - Cloud Functions Error: Function returned undefined, expected Promise or value, as well as TypeError: Cannot read property 'First_Name' of undefined TypeError:无法读取ES6模型上未定义的属性“ first_name” - TypeError: Cannot read property 'first_name' of undefined on es6 model [Vue 警告]:渲染函数中的错误:“TypeError:无法读取 null 的属性 &#39;first_name&#39;” - [Vue warn]: Error in render function: “TypeError: Cannot read property 'first_name' of null” “ TypeError:无法读取未定义的属性&#39;名称&#39;”错误 - “TypeError: Cannot read property 'name' of undefined” error POST请求错误-TypeError:无法读取未定义的属性“名称” - POST request error - TypeError: Cannot read property 'name' of undefined 反应错误类型错误:无法读取未定义的属性“名称” - React Error TypeError: Cannot read property 'name' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM