【问题标题】:Cannot read property 'push' of undefined Angular Firebase无法读取未定义的 Angular Firebase 的属性“推送”
【发布时间】:2019-03-16 19:08:47
【问题描述】:

** 无法读取未定义 Angular Firebase 的属性“push”**

import { Injectable } from '@angular/core';
import {FormControl,FormGroup, Validators} from '@angular/forms';
import {AngularFireDatabase, AngularFireList} from 'angularfire2/database';
@Injectable()
export class QuestionService {

  constructor(private firebase: AngularFireDatabase) { }
  questionList:AngularFireList<any>;


  form = new FormGroup({
    $key: new FormControl(null),
    newQuestion: new FormControl('', Validators.required)

  });

  getQuestions(){
    this.questionList = this.firebase.list('questions');
    return this.questionList.snapshotChanges();
  }

  insertQuestion(question){
    this.questionList.push({
      newQuestion: question.newQuestion
    });
  }

}

尝试将数据推送到 Firebase 但遇到此错误。

【问题讨论】:

  • 你没有初始化。这就是它发生的原因

标签: angular typescript firebase


【解决方案1】:

只需初始化您的列表

questionList:AngularFireList<any> = []

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-23
    • 2017-03-31
    • 2017-11-23
    • 2015-04-06
    • 1970-01-01
    • 2019-02-02
    • 2021-10-13
    • 2021-03-06
    相关资源
    最近更新 更多