node.js - 阵列更新后,Firestore 未显示更新的文档
问题描述
我在 Firebase 中有一个云函数,它使用此方法更新文档中的数组: https ://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array
但是,当我尝试获取文档的更新版本时,它会显示数组更新之前的内容。这是我的代码:
eventUpdated 在数组更新之前未显示正确的版本。
if(isPut(request)){
//All new additions
const userId = request.body.userId;
const eventId = request.body.eventId;
const attStatus = request.body.attStatus;
try {
let eventsRef = db.collection('events').doc(eventId);
if (attStatus === true) {
// Add a new ID to the "eventAttendanceList" array field.
let arrUnion = eventsRef.update({
eventAttendanceList: admin.firestore.FieldValue.arrayUnion(userId)
});
// Remove a ID from the "eventNonAttendanceList" array field.
let arrRm = eventsRef.update({
eventNonAttendanceList: admin.firestore.FieldValue.arrayRemove(userId)
});
}
else if (attStatus === false) {
// Add a new ID to the "eventNonAttendanceList" array field.
let arrUnion = eventsRef.update({
eventNonAttendanceList: admin.firestore.FieldValue.arrayUnion(userId)
});
// Remove a ID from the "eventAttendanceList" array field.
let arrRm = eventsRef.update({
eventAttendanceList: admin.firestore.FieldValue.arrayRemove(userId)
});
}
else {
//If you do anything other than true or false, it will just remove the userId from both attending and non-attending
let arrUnion = eventsRef.update({
eventAttendanceList: admin.firestore.FieldValue.arrayRemove(userId)
});
let arrRm = eventsRef.update({
eventNonAttendanceList: admin.firestore.FieldValue.arrayRemove(userId)
});
}
let eventUpdated = await db.collection('events').doc(eventId).get()
return response.status(200).json({status: "200", message: "Event RSVP successfully updated", event: eventUpdated.data()}).send;
} catch(err) {
return response.status(500).json({status: "500", message: "Event RSVP could not be updated"}).send;
}
} else {
return response.json({status: "ERR", message: "Please send a PUT request."}).send;
}
解决方案
您正在调用一组异步方法,但在调用方法并发送响应update()
之前,您无需等待它们完成。get()
所以你也应该使用await
forupdate()
方法,如下:
let eventsRef = db.collection('events').doc(eventId);
if (attStatus === true) {
// Add a new ID to the "eventAttendanceList" array field.
await eventsRef.update({
eventAttendanceList: admin.firestore.FieldValue.arrayUnion(userId)
});
// Remove a ID from the "eventNonAttendanceList" array field.
await eventsRef.update({
eventNonAttendanceList: admin.firestore.FieldValue.arrayRemove(userId)
});
}
else if (attStatus === false) {
//...
}
else {
//...
}
let eventUpdated = await db.collection('events').doc(eventId).get()
response.status(200).json({ status: "200", message: "Event RSVP successfully updated", event: eventUpdated.data() }).send;
我假设您使用的是HTTP Cloud Function。请注意,您无需执行任何操作return
,只需调用response.redirect()
、response.send()
或response.end()
。请参阅文档和相应的视频。
另请注意,如果您连续调用多个文档写入或更新,您可能会使用批量写入。
推荐阅读
- javascript - SQL Server 中的间距文本无法匹配
- ionic-framework - Http 调用在 iOS 上仅在蜂窝数据上不起作用 ionic-webview (1.2.1)
- wordpress - serving from http and https
- java - 切换活动和使用全局数组时出现 NullPointerException
- c++ - Cpp what is declaration of char a [10][100]
- java - Log4j 仅在启动期间旋转
- c++ - 只通过一次 if 语句
- java - 如何将 Scala Map 转换为 Java Map
- awk - 用于更新第 9 列和第 11 列和第 12 列的第 2 列基值的值
- javascript - 如果我需要多次解析一个文件,它会被多次解析吗?