【发布时间】:2018-12-08 22:58:44
【问题描述】:
我有一个简单的调用 Firestore 来编写一个文档,然后等待文档完成写入,然后再更改父级的状态。但是,父状态更改得太快,导致读取我认为尚未写入/传播的字段。我尝试使用 setTimeout 添加延迟,但似乎被忽略了。如何确保完全仅在 Firestore 文档编写完成后才调用状态更改?
代码:
updateDBEntry(stateObj) {
var that = this;
var docRef = firebase.firestore().collection('sessions').doc(this.state.userID);
docRef.get().then((doc) => {
if (!doc.exists) {
const timestamp = firebase.firestore.FieldValue.serverTimestamp();
var duration = (stateObj.seshDuration) ? stateObj.seshDuration : 1;
docRef.set({
seshName: stateObj.seshName,
seshStreet: stateObj.seshStreet,
seshZipcode: stateObj.seshZipcode,
seshDuration: duration,
seshDesc: stateObj.seshDesc,
seshTime: timestamp,
}).then(() => {
var handleToUpdate = that.props.handleToUpdate;
setTimeout(() => {
handleToUpdate(1); //this changes the parent's state
}, 10000);
});
}
});
}
【问题讨论】:
标签: react-native google-cloud-firestore