在 Firebase Cloud Functions onCreate 调用 setTimeout
Call setTimeout at Firebase Cloud Functions onCreate
我正在使用以下代码来调用带有 setTimeout 的函数,
但我不是很喜欢 JS 和 Firebase Cloud Functions。
但是我的日志显示以下错误:
“函数返回未定义的、预期的 Promise 或值”
“已完成函数的异常:错误:参数“documentPath”的值不是有效的资源路径。路径必须是非空字符串。”
这是我的代码:
function updateDoc(id) {
admin.firestore().collection('Orders').doc(id).update({status: 2}).then(() => {
console.log("Document successfully updated!");
})
.catch((error) => {
console.error("Error updating document: ", error);
});
}
exports.updateField = functions.firestore
.document('/Orders/{id}')
.onCreate((snapshot, context) => {
const id = context.params.id;
setTimeout(updateDoc.bind(id), 30000)
});
感谢您的帮助。
您的代码有几个问题:
- 您应该在调用
updateDoc()
方法时将 id 作为“简单”参数传递。
- 您需要 return Cloud Function 中的一个 Promise 或一个值,以向 Cloud Function 平台指示 Cloud Function 已完成。您可以在 doc and in this video series.
中找到更多详细信息
此外,在 Cloud Function 中使用 setTimeout
应小心,请参阅这些 SO answers。您可能 re-architect 您的解决方案以避免 setTimeout
。如果您分享有关使用它的原因的更多信息,社区可能会对您有所帮助。
我正在使用以下代码来调用带有 setTimeout 的函数, 但我不是很喜欢 JS 和 Firebase Cloud Functions。
但是我的日志显示以下错误: “函数返回未定义的、预期的 Promise 或值” “已完成函数的异常:错误:参数“documentPath”的值不是有效的资源路径。路径必须是非空字符串。”
这是我的代码:
function updateDoc(id) {
admin.firestore().collection('Orders').doc(id).update({status: 2}).then(() => {
console.log("Document successfully updated!");
})
.catch((error) => {
console.error("Error updating document: ", error);
});
}
exports.updateField = functions.firestore
.document('/Orders/{id}')
.onCreate((snapshot, context) => {
const id = context.params.id;
setTimeout(updateDoc.bind(id), 30000)
});
感谢您的帮助。
您的代码有几个问题:
- 您应该在调用
updateDoc()
方法时将 id 作为“简单”参数传递。 - 您需要 return Cloud Function 中的一个 Promise 或一个值,以向 Cloud Function 平台指示 Cloud Function 已完成。您可以在 doc and in this video series. 中找到更多详细信息
此外,在 Cloud Function 中使用 setTimeout
应小心,请参阅这些 SO answers。您可能 re-architect 您的解决方案以避免 setTimeout
。如果您分享有关使用它的原因的更多信息,社区可能会对您有所帮助。