HTTP 函数以代码 16 结束,文档未更新



>我有一个函数,它正确返回结果: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}`);
}
}
);

提前致谢

你在循环中调用response.send()。 这几乎肯定不是你想要的,因为你只能发送一个响应,然后函数终止。 将最后一个awaitresponse.send()移出循环。在所有工作完成后仅执行一次。

neighbors.forEach(async (neighbor) => {
// ...
});
await Promise.all(promises);
response.send("Update Last Payments Completed");

最新更新