Не удается прочитать свойство «push» из неопределенного в SafeSubscriber._next - PullRequest
0 голосов
/ 07 января 2019

Невозможно прочитать свойство 'push' из неопределенного на 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);

    });
  }

Я хочу опубликовать данные, которые я ввел в мою форму. Когда я нажимаю это, я получаю эту ошибку:

TypeError: Невозможно прочитать свойство 'push' из неопределенного на SafeSubscriber._next (studentfrm.component.ts: 42) в SafeSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.SafeSubscriber .__ tryOrUnsub

Ответы [ 3 ]

0 голосов
/ 07 января 2019

определить как ниже

<code><pre>
students = new Array<student>();

вместо определения как это students: student[];

0 голосов
/ 07 января 2019

Ваш student var не инициализирован. Заменить:

students: student[];

по:

students: students[] = [];
0 голосов
/ 07 января 2019

Просто инициализируйте вашу переменную.

    ngOnInit() {
        this.students = [];
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...