【发布时间】:2020-04-02 05:57:06
【问题描述】:
我使用 firebase 并创建触发器来监听学生文档的任何更改。如果学生文档发生更改,我会将任何更改推送到 rest api。但是我的函数调用了多次。
下面是我的功能:
exports.triggerTimeStudent= functions.firestore
.document('users/{userId}/class/{classId}/students/{studentId}')
.onUpdate( (change, context) => {
const newValue = change.after.data();
const previousValue = change.before.data();
const {update_at: afterStatus} = newValue;
const {update_at: beforeStatus} = previousValue;
const {name: name} = newValue;
if (afterStatus !== beforeStatus) {
try {
var data = {
"student_name": name,
};
console.log(data);
console.log("Inside Rest API");
return rest.post("https://example.com/api/v1/student/add-by-name", {
...studentServiceRestOption, ...{
body: JSON.stringify(data)
}
});
} catch (error) {
return res
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.send(buildError({
code: errorCode.SYSTEM_ERROR,
message: error.message,
}))
}
}
我不知道为什么函数调用多次。我只想打电话给时间。我的功能不正确。请帮忙
【问题讨论】:
-
我希望这个功能根本不起作用。您不能在 Firestore 函数中调用
res.send()。这仅适用于 HTTP 函数。 -
@DougStevenson 但它会在 example.com/api/v1/student/add-by-name 中创建成功学生姓名。它只在服务器出错时调用 res.send。它创造了成功,但它创造了两次。这意味着它两次调用触发器。我不知道为什么?请帮忙
-
@DougStevenson 当我删除 try catch 并只调用 rest 时,它创建了成功但创建了重复数据。当我在 Cloud Funtion 中查看日志时,它会调用多次触发
-
@DougStevenson 如何在触发器中调用rest api?
标签: node.js firebase google-cloud-platform google-cloud-firestore google-cloud-functions