我正在使用Firestore的批处理方法对多个集合(用户和事件)进行批处理更新。 我更新了事件集合中的2个字段(数组和数字字段),并更新了用户字段中的1个字段。
批处理更新是在几次get()调用之后进行的,以验证一些数据并在promise块内。
当我发送请求时,它会通过并更新collections对象,但请求一直持续到网关超时并返回408响应。
我在做什么错?如何解决此问题,以便发送请求并停止请求?
exports.markAttended = (req, res) => {
let batch = db.batch();
let eventDoc = db.doc(`/events/${req.params.eventId}`);
let userDoc = db.doc(`/users/${req.user.userName}`);
let attending = [];
let attendCount = 0;
let participants = [];
eventDoc
.get()
.then(doc => {
if (doc.empty) {
return res.status(404).json({ general: `Event does not exist` });
} else {
if (doc.data().attending === doc.data().cap) {
return res
.status(409)
.json({ general: `Event Full. It has a cap of ${eventDoc.cap}` });
}
eventUpdated = true;
let orgParticipants = doc.data().participants;
orgParticipants.push(req.user.userName);
participants = orgParticipants;
attendCount = doc.data().attending + 1;
return userDoc
.get()
.then(doc => {
if (doc.data().attending.includes(req.params.eventId)) {
return res
.status(409)
.json({ general: `Already attending this event` });
}
let orgAttending = doc.data().attending;
orgAttending.push(req.params.eventId);
attending = orgAttending;
batch.update(userDoc, { attending: attending });
batch.update(eventDoc, { participants: participants });
batch.update(eventDoc, { attending: attendCount });
return batch
.commit()
.then(() => {
return res.status(200);
})
.catch(err => {
return res.status(500).json({ error: err });
});
})
.catch(err => {
return res
.status(500)
.json({ error: `Error getting User. ${err}` });
});
}
})
.catch(err => {
return res.status(500).json({ error: `Error getting Event: ${err}` });
});
};