【发布时间】:2021-03-02 23:28:52
【问题描述】:
我正在尝试将记录保存到数据库中,如果该记录不在用户的个人资料中(他没有发现这个地方),而且该记录存在于所有地方的集合中。
我正在使用 Expo react native,我认为我的问题是,如果条件将在函数 recordInUsersAccount 和 recordInGlobalDatabase 之前执行。完成这两个功能后,有什么方法可以确保执行?在函数中,我在 state={} 中重写变量,所以我可以在下面检查它们。 (我尝试了 .then() 和 await, async 但我没有成功)。
非常感谢。
saveScannedQrCode(idOfPlace) {
this.recordInUsersAccount(idOfPlace);
this.recordInGlobalDatabase(idOfPlace);
if (!this.state.placeAlreadyScanned && this.state.placeExistInDatabase) {
// we will add this record into database
} else {
// we will NOT add this record into database
}
}
这是函数的代码:
recordInUsersAccount(idOfPlace) {
const userId = auth.currentUser.uid;
const usersRef = db
.collection("placesExploredByUsers") // default
.doc("mUJYkbcbK6OPrlNuEPzK") // default
.collection("s53sKFeF5FS0DjuI2cdO1Rp9sCS2") // uid
.doc(idOfPlace); // id of place
usersRef.get().then((docSnapshot) => {
if (docSnapshot.exists) {
this.setState({
placeAlreadyScanned: true, // place is in user's database
});
} else {
this.setState({
placeAlreadyScanned: false, // place is NOT in user's database
});
}
});
}
recordInGlobalDatabase(idOfPlace) {
const usersRef = db
.collection("databaseOfPlaces") // default
.doc(idOfPlace); // id of place
usersRef.get().then((docSnapshot) => {
if (docSnapshot.exists) {
this.setState({
placeExistInDatabase: true, // place is in global database of places
});
} else {
this.setState({
placeExistInDatabase: false, // place is NOT in global database of places
});
}
});
}
【问题讨论】:
-
你能发布你想要等待的函数的代码吗?
-
当然,我更新了我的帖子
标签: react-native expo