这是我用来访问 firebase 数据的函数。
export class SubscriptionProductComponent implements OnInit {
private products: any;
private customer: any;
constructor(private database: AngularFireDatabase,private route: Router) {
this.customer = this.database.database.ref().child('/Products/');
this.customer.orderByChild("productType").equalTo("subscription").on('value', function (snapshot) {
console.log(snapshot.toJSON());
this.products = snapshot.toJSON();
console.log(this.products);
});
}
ngOnInit() {}
//Some other function are here
}
我收到错误
core.js:6210 ERROR TypeError: Cannot set property 'products' of undefined
at subscription-product.component.ts:34
at valueCallback (index.esm.js:14463)
at CallbackContext.push.6Uf2.CallbackContext.onValue (index.esm.js:12274)
at index.esm.js:13118
at exceptionGuard (index.esm.js:638)
at eventListRaise (index.esm.js:11082)
at eventQueueRaiseQueuedEventsMatchingPredicate (index.esm.js:11057)
at eventQueueRaiseEventsForChangedPath (index.esm.js:11044)
at repoOnDataUpdate (index.esm.js:11261)
at PersistentConnection.repo.server_ [as onDataUpdate_] (index.esm.js:11163)
请帮我解决这个问题。
答案 0 :(得分:1)
用箭头函数来做:
this.customer.orderByChild("productType").equalTo("subscription").on('value', (snapshot) => {
console.log(snapshot.toJSON());
this.products = snapshot.toJSON();
console.log(this.products);
});