Firebase Firestore函数。onCreate触发器

Firebase Firestore函数。onCreate触发器,firebase,google-cloud-firestore,google-cloud-functions,Firebase,Google Cloud Firestore,Google Cloud Functions,我正在使用.onCreate对集合发送通知 我的收藏是tasks。当新任务下达时,我需要向指定人员发送通知 我是否需要保存此指定人员的令牌?我只能获取保存在文档中的值。token的值位于users集合中 有没有办法在函数中使用。where条件 到目前为止我的代码[在assignTo字段中保存每个新任务条目中分配的人员的令牌]: .document('todos/{todosId}') .onCreate( async (snapshot: { data: () =

我正在使用
.onCreate
对集合发送通知

我的收藏是
tasks
。当新任务下达时,我需要向指定人员发送通知

我是否需要保存此指定人员的
令牌
?我只能获取保存在文档中的值。
token
的值位于
users
集合中

有没有办法在函数中使用
。where
条件

到目前为止我的代码[在
assignTo
字段中保存每个新任务条目中分配的人员的令牌]:

    .document('todos/{todosId}')
    .onCreate(
        async (snapshot: { data: () => { (): any; new(): any; token: any; assignTo: any; text: any; name: any; profilePicUrl: any; }; }) => {

          // Notification details.
          const text = snapshot.data();
          const payload = {
            notification: {
            title: text.name,
             body: 'Notification body',
             icon: 'https://img.icons8.com/material/4ac144/256/user-male.png',
             click_action: `https://google.com`,
            }
          }; 

     const subscriber =   text.token;
    return admin.messaging().sendToDevice(subscriber, payload)
  });
。在哪里
条件可能?像

集合“用户”获取令牌where
assignTo
=
dwqhdiu78798u

编辑 这里是我的代码,但函数给出了错误

    //  const subscriber = "evGBnI_klVQYSBIPMqJbx8:APA91bEV5xOEbPwF4vBJ7mHrOskCTpTRJx0cQrZ_uxa-QH8HLomXdSYixwRIvcA2AuBRh4B_2DDaY8hvj-TsFJG_Hb6LJt9sgbPrWkI-eo0Xtx2ZKttbIuja4NqajofmjgnubraIOb4_"; 
    const query = admin.firestore().collection('Students').where('Name', '==', 'caca');
    const querySnapshot = await query.get();
    const subscriber = querySnapshot.doc.data().token;
    return admin.messaging().sendToDevice(subscriber, payload)
  });
附截图:

功能错误:

如果您的问题是“在云函数中,我是否可以通过包含子句的
查询
获取Firestore数据”,答案是肯定的

您应该像发送通知一样使用Admin SDK,如下所示:

const query = admin.firestore().collection('users').where('assignTo', '==', 'dwqhdiu78798u');
const querySnapshot = await query.get();
//....

根据您的评论和更新进行更新 您在以下行(添加到问题中的代码中)出现错误:


这是因为
QuerySnapshot
没有
doc
属性。
QuerySnapshot
包含一个或多个文档快照。您应该使用
docs
数组或使用
forEach()

谢谢您的回复。我用
db
的屏幕截图更新了我的问题,我尝试了
code
,它给出了
函数中的错误
,还添加了错误的屏幕截图。所有工作,在日志中我看到此消息-
未配置计费帐户。外部网络无法访问,配额受到严重限制。配置计费帐户以删除这些限制
,这是否意味着我无法调用任何外部api(如短信或电子邮件)?您需要加入Blaze计划,请参阅
const subscriber = querySnapshot.doc.data().token;