使用云函数 onCreate 从 firebase firestore 中 onCreate 的集合/文档中检索数据

零点

我正在尝试从 onCreate 内的 cloud firestore 中的用户集合中检索设备令牌。d.get("token"); 下面返回一个未定义的。

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

admin.initializeApp(functions.config().firebase);

const fcm = admin.messaging();

exports.senddevices = functions.firestore
    .document("notification/{id}")
    .onCreate((snap, context) => {
      const name = snap.get("name");
      const subject = snap.get("subject");

      return admin.firestore()
          .doc("users/{id}")
          .get()
          .then((d) => {
            if (!d.empty) {
              const payload = {
                notification: {
                  title: "from " + name,
                  body: "subject " + subject,
                  sound: "default",
                },
              };

              d.

              const token = d.get("token");

              return fcm.sendToDevice(token, payload);
            } else {
              console.log("User not found");
            }
          });
    });

达马拉吉

您需要从.document()方法中提供的路径中读取 ID 的值,然后在.doc()方法中使用它来获取文档,如下所示:

exports.senddevices = functions.firestore
    .document("notification/{id}")
    .onCreate((snap, context) => {
      const name = snap.get("name");
      const subject = snap.get("subject");
      const id = context.params.id
      //    ^^ reading value of ID
      return admin.firestore()
          .doc("users/"+id) // <-- Using ID, 
          //or .doc(`users/${id}`)
          .get()
          .then((d) => {
            if (d.exists) {
              const payload = {
                notification: {
                  title: "from " + name,
                  body: "subject " + subject,
                  sound: "default",
                },
              };

              const token = d.data().token;

              return fcm.sendToDevice(token, payload);
            } else {
              console.log("User not found");
            }
          });
    });

请注意您正在获取单个文档,因此您应该使用它d.exists来检查文档是否存在,而不是.empty在 QuerySnapshot 上使用。同样要读取令牌字段的值,您应该首先使用.data()方法将该文档的内容作为对象获取。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

NullPointerException访问onCreate()中的视图

在oncreate或onresume中获取menuitem

FIrebase Firestore onCreate云功能事件参数未定义

比较Firebase数据库的云函数触发器onCreate(),onWrite(),onUpdate(),何时使用?

onCreate中的函数并非每次都执行

onCreate不在服务中运行

'onCreate'Firebase云功能错误

为什么在Firestore中触发onCreate在子集合中的新文档后添加新字段时总是得到NaN?

.onCreate云函数触发后如何更新或设置Firestore文档的属性

您如何判断用户在onCreate firebase云功能中是否“匿名”?

使用Firestore触发onCreate更改单词

Firebase函数onCreate不触发

空值onCreate()中的Extras

onCreate()中的startActivity

Android的onCreate中的NullPointerException

oncreate()中的onclick列表视图

在onCreate()函数中添加行

onCreate()中的OnClickListener()?

onCreate()方法中的View动画

oncreate Application 类中的 IllegalStateException

Firebase 函数 onCreate 不会写入 firestore 但 https.onRequest 会 - 相同的代码

Firebase 函数 onCreate user

如何在firebase中创建两个onCreate函数

在“onCreate”谷歌云功能(firestore)之后获取文档字段

Firebase 云函数 - null user.displayName onCreate

我在 firebase 函数中的 onCreate 函数没有在云数据库中创建我想要的集合

使用 Firestore 和 Firebase 云函数,您如何迭代具有嵌套在子集合中的特定 ID 的文档?

如何在 onCreate 函数中访问 TextView?

使用 Cloud Functions 从 onCreate 触发器中检索 Firestore 文档