Impossible de lire la propriété 'push' d'undefined sur SafeSubscriber._next
import { Component, OnInit } from '@angular/core'; import {StudentService} from '../student.service'; import {student} from '../student'; @Component({ selector: 'app-studentfrm', templateUrl: './studentfrm.component.html', styleUrls: ['./studentfrm.component.css'], providers:[StudentService] }) export class StudentfrmComponent implements OnInit { students: student[]; student: student; name: string; birthdate: string; gender: string; address: string; guardianname: string; contact: string; email: string; Areainterested: string; constructor(private studentservice: StudentService) { } addstudent() { const newstudent = { name: this.name, birthdate: this.birthdate, gender: this.gender, address: this.address, guardianname: this.guardianname, contact: this.contact, email: this.email, Areainterested: this. Areainterested } this.studentservice.addstudent(newstudent) .subscribe(student =>{ this.students.push(student); // this.studentservice.getstudents() // .subscribe(students => // this.students = students); }); }Je souhaite publier les données que j'entre dans mon formulaire. Quand je pousse ça, j'ai cette erreur:
TypeError: impossible de lire la propriété 'push' d'undefined sur SafeSubscriber._next (studentfrm.component.ts: 42) sur SafeSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.SafeSubscriber .__ tryOrUnsub
3 Réponses :
Initialisez simplement votre variable.
ngOnInit() { this.students = []; }
ou faites-le simplement en ligne: étudiants: étudiant [] = [];
définir comme ci-dessous
<pre> students = new Array<student>(); </pre>
au lieu de définir comme ceci étudiants: étudiant [];
Votre variable étudiant
n'est pas initialisée. Remplacer:
students: students[] = [];
par:
students: student[];
initialisez simplement
ces étudiants: les étudiants [] = [];
devrait fonctionner