简体   繁体   English

Firebase 功能:在 function 中获取文档数据

[英]Firebase functions: getting document data inside a function

im making a type of chat app.我正在制作一种聊天应用程序。 Im trying to write a firestore function that listens to a specific document that, when updated, will trigger the function to send a push notification to a specific user.我正在尝试编写一个 Firestore function 来侦听特定文档,该文档在更新时将触发 function 向特定用户发送推送通知。 However, im receiving an error.但是,我收到一个错误。 So far, i have:到目前为止,我有:

export const messageListener = functions.firestore
    .document("stingrayMessageListener/stingrayMessageListener")
    .onWrite(async (change) => {
      const beforeData = change?.before?.data();
      const afterData = change?.before?.data();
      if (beforeData?.receiverId!=afterData?.receiverId) {
        const docRef = admin.firestore().doc("users/${afterData?.receiverId}");
        const docSnap = await docRef.get();
        const payload = {
          notification: {
            title: "New message!",
            body: "${afterData?.senderName} has sent you a message!",
          },
          data: {
            body: "message",
          },
        };
        admin.messaging().sendToDevice(docSnap.data()?.token, payload);
      }
    });

This function is spitting out the following error:这个 function 吐出以下错误:

Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array提供给 sendToDevice() 的注册令牌必须是非空字符串或非空数组

Im pretty sure this is implying that docSnap() is returning as null, because token is a field on all user documents.我很确定这意味着 docSnap() 以 null 的形式返回,因为令牌是所有用户文档上的一个字段。 Am I doing something wrong?难道我做错了什么?

In addition to using template literals in your document paths as you answered, both beforeData and afterData are assigned to change?.before?.data() , meaning your function won't send notifications to any device:除了在您回答时在文档路径中使用模板文字外, beforeDataafterData都被分配为change?.before?.data() ,这意味着您的 function 不会向任何设备发送通知:

const beforeData = change?.before?.data();
const afterData = change?.before?.data();
if (beforeData?.receiverId != afterData?.receiverId) {
    //above condition won't be true
...

You'd only need to use the after property which holds the changed document.您只需要使用保存更改后的文档的after属性。

const afterData = change?.after?.data();

Let me know if this was helpful.让我知道这是否有帮助。

So, turns out it was a very simple solution.所以,事实证明这是一个非常简单的解决方案。 In Typescript, you can dont use "" to use a variable in a string.在 Typescript 中,不能使用 "" 来使用字符串中的变量。 You use ``.你用```。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 angular 在 Firebase Cloud Functions (Firestore) 中获取单个文档 ID - Getting single document ID in Firebase Cloud Functions (Firestore) with angular Firebase 函数:参数“数据”的值不是有效的 Firestore 文档 - Firebase functions: Value for argument "data" is not a valid Firestore document Firestore接管一分钟搞定里面的文档 Firebase Cloud Function - Firestore take over a minute to get a document inside Firebase Cloud Function Firebase function 没有在带有 DispatchGroup 的 forEach 循环中被调用 - Firebase function not getting called inside a forEach loop with a DispatchGroup 数据未通过调度程序 function 和 Firebase Cloud Functions 保存到 Firestore - Data not saved into Firestore through scheduler function with Firebase Cloud Functions 将 firebase 存储中的图像连接到 firebase 中的数据文档 - Connecting an image in firebase storage, to a data document in firebase 使用 Firebase 函数时获取“函数返回未定义,预期 Promise 或值” - Getting "Function returned undefined, expected Promise or value " while using Firebase Functions 我应该分页数据查询吗? 从 firebase 函数中获取 5000 个文档 - Should I paginate data query? Getting 5000 documents from firebase functions firebase firestore 在事务中添加新文档 - transaction.add 不是 function - firebase firestore adding new document inside a transaction - transaction.add is not a function 来自 Firebase 文档的数据总和 - Sum of data from a firebase document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM