我有一个函数,可以使用以下命令正确返回结果:response.send("Update Last Payments Completed");
,但在日志中报告:并且没有文档被更新
错误:进程退出,代码为16
这是我的代码:
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
/// Updates the last payment done in the neighbors documents
export const updateLastPaymentHTTP = functions.https.onRequest(
async (request, response) => {
try {
const neighbors = await admin.firestore().collection("neighbors").get();
const promises = [];
neighbors.forEach(async (neighbor) => {
const topPayment = await admin
.firestore()
.collection(`neighbors/${neighbor.ref}/payments`)
.orderBy("date", "desc")
.limit(1)
.get();
topPayment.forEach(async (payment) => {
if (payment.exists) {
const lastPayment = payment.data().date;
promises.push(neighbor.ref.update({ last_payment: lastPayment }));
} else {
promises.push(neighbor.ref.update({ last_payment: null }));
}
});
await Promise.all(promises);
response.send("Update Last Payments Completed");
});
} catch (error) {
console.log(`Error Updating Last Payment and Debt ${error}`);
}
}
);
预先感谢
答案 0 :(得分:1)
您正在循环内调用response.send()
。几乎可以肯定这不是您想要的,因为您只能发送一个响应,然后函数终止。将最后一个await
和response.send()
移动到循环中的外部。在所有工作完成后只能执行一次。
neighbors.forEach(async (neighbor) => {
// ...
});
await Promise.all(promises);
response.send("Update Last Payments Completed");