【发布时间】:2021-07-21 12:06:25
【问题描述】:
已经实现了一个firebase cloud函数,每次price的值变化都会触发,代码如下:
export const orderListener = functions.firestore
.document("/users/{userId}/Order/{orderId}")
.onUpdate(async (change, context) => {
const { userId, orderId } = context.params;
const price = change.after.data().price;
try {
if (price !== 0) {
const userDoc = await admin
.firestore()
.collection(`/users/${userId}/Personal information`)
.doc("Personal Information")
.get();
const { fcmToken } = userDoc.data()!;
functions.logger.log({
userId,
fcmToken,
orderId,
price,
});
//RETURN
return admin.messaging().sendToDevice(fcmToken, {
notification: {
title: "Title of the message",
body: `${orderId}'s price > 0`,
},
});
} else {
return null
}
} catch (e) {
functions.logger.error(e.message);
//RETURN
return null
这段代码已经部署在firebase函数中,我尝试通过更改价格值来测试它,并通过查看firebase控制台来监控执行。
期望:
一旦我更改了我的 firestore 数据库中的 price 值,它几乎立即执行(在 5 秒内)。
结果:
该函数执行得很好,并已登录到 Firebase 控制台,但在我更新价格值后执行有 50 分钟延迟(函数执行仅在 50 分钟后记录)。别人测试过,好像没有这个问题。
问题:
此代码中包含任何潜在错误? firebase 设置、设置等是否可能有一些错误...?
- 编辑:感谢 Dharmaraj 的贡献,我用返回语句更新了上面的代码。但问题仍然存在。
【问题讨论】:
-
我会尝试在
await admin.messaging()之前添加return,并在每个catch块中添加return null以防出错。 -
谢谢你的回复,你写几行代码可以说明你的意思吗?
-
尝试从Gist复制代码我添加了return语句并删除了多余的try-catch
-
谢谢。但是这段代码给出了错误消息 onUpdate( async (change, context) => { .... return ...} with Not all code paths return a value.
-
我刚刚编辑了要点。立即尝试。
标签: firebase google-cloud-functions