无法读取 SafeSubscriber._next 处未定义的属性“push”

vis*_*nod 6 typescript angular angular7

无法读取 SafeSubscriber._next 处未定义的属性“push”

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

    });
  }
Run Code Online (Sandbox Code Playgroud)

我想发布我输入到表单中的数据。当我推动它时,我收到此错误:

TypeError:无法在 SafeSubscriber._next (studentfrm.component.ts:42) 处读取 SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub 处未定义的属性“push”

Arc*_*ezy 2

只需初始化您的变量即可。

    ngOnInit() {
        this.students = [];
    }
Run Code Online (Sandbox Code Playgroud)