Javascript 无法读取属性';推动';使用angular 5在firebase中存储数据时未定义的
这就是我目前的情况:Javascript 无法读取属性';推动';使用angular 5在firebase中存储数据时未定义的,javascript,angular,firebase,firebase-realtime-database,ionic3,Javascript,Angular,Firebase,Firebase Realtime Database,Ionic3,这就是我目前的情况: createPerson(name:any,lname:any,age:any,dept:any){ this.peopleList.push({ name: name, lname: lname, age: age, dept: dept }).then(_person =>{ this.navCtrl.push(HomePage); },error=>{console.log(error)});} 这种语法似乎很有前途。承诺可以是p
createPerson(name:any,lname:any,age:any,dept:any){
this.peopleList.push({
name: name,
lname: lname,
age: age,
dept: dept
}).then(_person =>{
this.navCtrl.push(HomePage);
},error=>{console.log(error)});}
这种语法似乎很有前途。承诺可以是
promise
类型,但这并不意味着您可以直接使用push()
此外,假设this.peopleList
是任何[]
,则需要先初始化它,然后才能执行this.peopleList.push()
最后,您似乎想在之后做些什么。peopleList
收到一个新项目。你应该有一个可观察/承诺,并分两个阶段进行。它添加到数组并返回带有新值的数组,然后在第一个任务完成后使用。然后或。订阅执行操作
编辑:
export interface AngularFireList<T> {
query: DatabaseQuery;
valueChanges(events?: ChildEvent[]): Observable<T[]>;
snapshotChanges(events?: ChildEvent[]): Observable<SnapshotAction<T>[]>;
stateChanges(events?: ChildEvent[]): Observable<SnapshotAction<T>>;
auditTrail(events?: ChildEvent[]): Observable<SnapshotAction<T>[]>;
update(item: FirebaseOperation, data: T): Promise<void>;
set(item: FirebaseOperation, data: T): Promise<void>;
push(data: T): database.ThenableReference;
remove(item?: FirebaseOperation): Promise<void>;
}
检查“peopleList”是否已定义Yes已定义,代码是导出类AboutPage{peopleList:AngularFireList构造函数(public navCtrl:NavController,public db:AngularFireDatabase){this.peopleList=db.list('/people');};在本例中,我没有使用push()直接来说,我已经将peopleList初始化为AngularFireList,但仍然出现错误,您可以在下面查看我的代码…期待收到您的回复///初始化peopleList的代码是=导出类AboutPage{peopleList:AngularFireList构造函数(public navCtrl:NavController,public db:AngularFireDatabase){this.peopleList=db.list('/people');};你能提供一个plunkr或repo来更清楚地看到这个问题吗?调试时,是什么抛出了未定义的?this.navCtrl.push(HomePage);
这也需要初始化才能被推送。它们是正确的类型吗?
// It subscribes to the changes on the list and perform whatever you need upon change.
// Put it after you initialized the peopleList
this.peopleList.valueChanges.subscribe((data) => {
this.navCtrl.push(HomePage);
}, error => console.log(error));
// This is adding a new item on the list, which will trigger the subscription above
this.peopleList.push({
name: name,
lname: lname,
age: age,
dept: dept
});