我使用一个名为未说明的状态库,当我运行以下代码时:
import { Container } from 'unstated';
import Fire from '../Core/Fire';
class Store extends Container {
state = {
users: [],
};
getAllUsers() {
const db = Fire.firestore();
const reference = db.collection('users');
reference
.get()
.then(snapshot => {
const docs = [];
snapshot.forEach(doc => {
docs.push(doc);
});
this.setState({
users: docs.map(doc => {
return {
id: doc.id,
model: doc.data(),
};
}),
});
})
.catch(error => {
console.error(error);
});
}
}
export default Store;我知道这个错误:
TypeError:无法读取Store.js:19处未定义的属性“setState”
我肯定会得到结果,因为docs在设置状态时确实包含对象。在文档/示例中,我也在做完全相同的事情。
为什么没有看到参考this?是因为它在承诺中吗?哈!
发布于 2018-06-20 13:39:50
您应该绑定函数或使用箭头函数。
1.)
constructor(){
this.getAllUsers=this.getAllUsers.bind(this)
}2)
getAllUsers = () =>{ ... }https://stackoverflow.com/questions/50946867
复制相似问题