javascript Firestore TypeError: невозможно прочитать свойство 'state' undefined - PullRequest
0 голосов
/ 13 июля 2020

TypeError: Невозможно прочитать свойство 'state' undefined. Я попытался получить pr_cluster документа в firestore, но возникла ошибка.

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;
                    
                    
                            });
                            
                
            });

1 Ответ

1 голос
/ 13 июля 2020

Вы должны использовать функцию стрелки, чтобы избежать переопределения this внутри обратного вызова forEach.

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