错误TypeError:无法读取未定义angular6的属性“推”

时间:2019-08-29 03:40:28

标签: firebase angular6

下面有一段代码:

getRegistrations(){
  this.registrationList = this.firebase.list('registrations');
  return this.registrationList.snapshotChanges();
}

insertRegistration(registration){
  this.registrationList.push({
    firstname: registration.firstname,
    lastname: registration.lastname,
    phone: registration.phone,
    email: registration.email,
    password: registration.password
  });
}

出现以下错误:

  

错误TypeError:无法读取未定义的属性“ push”

2 个答案:

答案 0 :(得分:0)

尝试一下

return arr.reduce((a,b) => a.length < b.length ? b : a, "");

答案 1 :(得分:0)

  

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

这是因为在您呼叫 this.registrationList 时或未初始化时, undefined 得到了insertRegistration()。< / p>

您可以检查该值是否存在,如果是undefined,则为其分配一个空数组。

  getRegistrations() {
     this.registrationList = this.firebase.list('registrations');
     return this.registrationList.snapshotChanges();
  }

  insertRegistration(registration) {
     this.registrationList = this.registrationList : this.registrationList ? [];
     this.registrationList.push({
       firstname: registration.firstname,
       lastname: registration.lastname,
       phone: registration.phone,
       email: registration.email,
       password: registration.password
    });
  }

我希望这会起作用..:)