Aou*_*ane 1 javascript node.js firebase google-cloud-functions google-cloud-firestore
我知道这个问题已经问过了,但没有帮助。我有一个“聊天”集合,其中包含一个文档“成员”和一个子集合“消息”,我想在子集合中添加新消息时触发云功能。
这是我尝试过的,但它仅在“成员”更新时触发,并且没有关于子集合的任何信息:
exports.chatsCollectionTriggers = functions.firestore.document('/chats/{chatId}/messages/{messageId}').onUpdate(async (change, context) => {
let chatBefore = change.before.data();
let chatAfter = change.after.data();
console.log(JSON.stringify(chatBefore, null, 2));
console.log(JSON.stringify(chatAfter, null, 2));
console.log(context.params.chatId);
console.log(context.params.messageId);});
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在子集合更新时触发云功能?
当您修改chats集合的文档时(例如,如果您修改文档的members字段),您的 Cloud Function 将不会被触发G162R...。
当您修改(而不是创建)集合中文档的messages子集合中的文档时,将触发您的云函数chats。例如,如果您更改text消息文档的字段值vVwQXt....。
所以,回答你的问题
我的问题是如何在子集合更新上触发云功能
如果“子集合更新”是指更新子集合中的现有文档,则您的云功能是正确的。
如果“子集合更新”你的意思是创建一个的新的子集合文件(可以是“一个子集合更新”的一种解释),你应该从改变你的触发类型onUpdate()来onCreate()。
从您问题中的以下句子,即“我想在子集合中添加新消息时触发云功能”,似乎您想要第二种情况,因此您应该将云功能调整为如下:
exports.chatsCollectionTriggers = functions.firestore.document('/chats/{chatId}/messages/{messageId}').onCreate(async (snap, context) => {
const newValue = snap.data();
console.log(newValue);
console.log(context.params.chatId);
console.log(context.params.messageId);
return null; // Important, see https://firebase.google.com/docs/functions/terminate-functions
})
Run Code Online (Sandbox Code Playgroud)
文档中的更多详细信息。
| 归档时间: |
|
| 查看次数: |
590 次 |
| 最近记录: |