【问题标题】:how to write function for a specific document in firestore?如何为firestore中的特定文档编写函数?
【发布时间】:2022-01-02 22:56:02
【问题描述】:

以下是我在youtube上使用的功能。

     const functions = require("firebase-functions");
     const admin = require("firebase-admin");

     admin.initializeApp();

     exports.androidPushNotification = 
     functions.firestore.document('MyMoneyNotifications/{MyPercentageMoney}').onCreate(

     (snapshot, context)=>
      {
         admin.messaging().sendToTopic(
      "new_user_forums",
   {
     notification:{
         title:snapshot.data().title,
         body: snapshot.data().body
                   }
            });});

它工作正常。 但我想检查以下结构。

        Notifications(collection)
              ->UserId(document)
                 ->MyNotification(collection)
                              ->notify1
                              ->notify2

现在,我想检查特定用户是否有任何新通知。如何在 firebase 函数中检查集合“MyNotification”

【问题讨论】:

    标签: node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    如果您尝试收听子集合,则可以将路径设置为:

    exports.androidPushNotification = 
         functions.firestore.document('Notifications/{userId}/MyNotification/{notificationId}')
    

    您可以在documentation 中阅读更多相关信息

    【讨论】:

    • @LTR snapshot 变量包含新创建文档的数据。您可以通过snapshot.data()访问它。
    • 我想获取通知的用户令牌。创建通知文档后,我需要向特定用户发送通知。如何从文档中获取数据?我的数据库:用户(集合)-> userId(文档)->令牌(字段)。我必须从快照数据中获取用户 ID。 val userId=snapshot.data().userid;从这个用户 ID,我必须从用户集合中的用户文档中获取令牌。而且我必须使用该特定令牌发送通知
    • @LTR 你可以通过db.collection('users').doc(snapshot.data().id).get() 这样做,然后从中读取令牌字段。
    • 如果这不起作用,请发布一个包含更多详细信息的新问题,并且它会比 cmets 更好。
    猜你喜欢
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2021-11-28
    • 2019-04-10
    • 2021-08-17
    相关资源
    最近更新 更多