javascript firestore 类型错误:无法读取属性'state'未定义



类型错误:无法读取未定义的属性"state"。我试图在firestore中获取文档的pr_cluster,但出现了错误。

this.props.firebase.cartItems().doc(authUser.uid).collection('products').limit(1).onSnapshot(function(querySnapshot) {


querySnapshot.forEach(function(doc) {

this.state.pr_cluster = doc.data().pr_cluster;


});


});

您应该使用箭头函数来避免在forEach回调中重新定义this

querySnapshot.forEach(doc => {
this.state.pr_cluster = doc.data().pr_cluster;
});

相关内容

最新更新