【问题标题】:How to create a Firebase Cloud Function for a Document Created on a Collection inside of another Document and Collection如何为在另一个文档和集合内的集合上创建的文档创建 Firebase 云函数
【发布时间】:2021-06-21 17:08:12
【问题描述】:

我对 CloudFunctions 有疑问,我需要在 Firestore 内创建文档时创建一个函数。

我遇到的问题是数据的设置方式:

所以我有一个名为 Chat Rooms 的集合,它获取的文档会有所不同,并且在每个文档中,您将有一些字段和另一个子集合,并且在该子集合中已经是我需要使用该功能的东西。

我遇到的问题是这个函数应该知道或访问每个创建的文档:

有点像:

exports.ChatSent = functions.firestore.document('chatrooms/{Variable part}/chats').onCreate((snap, ctx) => { print('do whatever'); return;});

问题是我不知道如何编写变量部分,因为每当在每个文档的 Chats 集合上写入新文档时,都应该执行此函数Chatroom 集合中的文档。

有什么想法吗?

【问题讨论】:

    标签: firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    你应该使用两次wildcard when defining the path,如下:

    exports.ChatSent = functions.firestore.document('chatrooms/{chatroomId}/chats/{chatId}').onCreate((snap, ctx) => {
    
     print('do whatever');
     return null;
    
    });
    

    如果您需要获取通配符值,请执行以下操作:

    exports.ChatSent = functions.firestore.document('chatrooms/{chatroomId}/chats/{chatId}').onCreate((snap, ctx) => {
    
     const chatroomId = ctx.params.chatroomId;
     const chatId = ctx.params.chatId;`
    
     print('do whatever');
     return null;
    
    });
    

    【讨论】:

    • 非常感谢我所需要的。
    • 很高兴能帮到你!如果它解决了您的问题,您也可以支持我的回答,请参阅stackoverflow.com/help/someone-answers。谢谢
    • 非常感谢
    猜你喜欢
    • 2019-09-18
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 2012-10-07
    • 2021-02-20
    • 1970-01-01
    相关资源
    最近更新 更多