Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'push' of undefined at SafeSubscriber._next

Cannot read property 'push' of undefined at 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);

    });
  }

I want to post the data that I enter to my form. When I push that, I got this error:

TypeError: Cannot read property 'push' of undefined at SafeSubscriber._next (studentfrm.component.ts:42) at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub

like image 722
vishnu vinod Avatar asked Oct 16 '22 08:10

vishnu vinod


1 Answers

Just initialize your variable.

    ngOnInit() {
        this.students = [];
    }
like image 179
Arcteezy Avatar answered Oct 19 '22 15:10

Arcteezy