【发布时间】:2021-09-14 02:50:41
【问题描述】:
我的应用程序使用 Firestore Function Triggers 根据 Firestore 中的更改执行后台操作,例如用户配置文件更改。例如,如果他们更改了手机号码,则会发送验证码。
我有一个触发器,当特定集合上发生onWrite() 事件时应该运行该触发器。 onWrite() 在 Firebase 中针对特定集合发生以下任何操作时运行:
在我的用例中,我需要它为onCreate() 和onUpdate() 运行,因此我使用onWrite()
对于 Firebase Triggers to work,除了表示已创建/更改/删除的文档的文档 ID/通配符之外,还需要特定格式。
常量:
const collections = {
...
conversations: "conversations",
...
}
可调用函数(更新 firestore):
/**
* Add an conversation to conversations collection
* @type {HttpsFunction & Runnable<any>}
*/
exports.addConversations = functions.https.onCall(async (data, context) => {
// expects conversation & interested state
const {valid, errors} = validateRequiredBody(data, [
"conversation",
]);
if (!valid) {
return {
status: false,
message: "Missing or invalid parameters",
errors: errors,
jwtToken: "",
};
}
// get conversation item
const conversation = {
id: data["conversation"]["id"],
name: data["conversation"]["name"],
}
// create conversation with empty counter
// let writeResult = await collectionRefs.conversationsRef.doc(conversation.id).set({
let writeResult = await admin.firestore().collection(collections.conversations).doc(conversation.id).set({
id: conversation.id,
name: conversation.name,
count: 0
});
console.log(`[function-addConversations] New Conversation [${conversation.name}] added`);
return {
status: true,
message: ""
}
});
Firestore 触发器(未触发):
/**
* On conversations updated/removed, update corresponding counter
* @type {CloudFunction<Change<QueryDocumentSnapshot>>}
*/
exports.onConversationProfileCollectionCreate = functions.firestore.document(`${collections.conversations}/{id}`)
.onWrite(async snapshot => {
console.log("Conversation Collection Changed");
// let conversation = collectionRefs.conversationsRef.doc(snapshot.id);
// await conversation.update({count: FieldValue.increment(1)});
});
在我的移动应用程序中,用户(调用)addConversations() firebase 函数,这会将新对话添加到清晰可见的 Firestore,但计数器触发器(触发函数)不运行。
模拟器输出:
...
{"verifications":{"app":"MISSING","auth":"MISSING"},"logging.googleapis.com/labels":{"firebase-log-type":"callable-request-verification"},"severity":"INFO","message":"Callable request verification passed"}
[function-addConversations] New Conversation [Test Conversation Topic] added
Profile updated
(print profile data)
...
我应该看到的:
...
{"verifications":{"app":"MISSING","auth":"MISSING"},"logging.googleapis.com/labels":{"firebase-log-type":"callable-request-verification"},"severity":"INFO","message":"Callable request verification passed"}
[function-addConversations] New Conversation [Test Conversation Topic] added
Conversation Collection Changed // this is output by the trigger
Profile updated
(print profile data)
...
我是不是做错了什么?
【问题讨论】:
-
你从日志中的函数执行中看到了什么吗?例如。
onConversationProfileCollectionCreate Function execution started -
嗨@RenaudTarnec 没有。我有一个配置文件
onWrite()触发器输出Profile Updated,但标准终端输出中没有其他内容。您对我如何启用详细日志记录有什么建议吗? -
如果您尝试部署 Cloud Functions 会发生什么?
-
可能是集合名称声明不正确或其他原因。您是否检查过
${collections.conversations}是否具有您期望的值?另外,firebase-debug.log 中是否出现任何错误? -
当你在你的系统上测试这个时,你确定你的云函数是writing to the emulators而不是你的实时数据库吗?
标签: node.js firebase google-cloud-firestore google-cloud-functions