FIrebase Firestore onCreate云功能事件参数未定义 [英] FIrebase Firestore onCreate Cloud Function Event Params Undefined

查看:37
本文介绍了FIrebase Firestore onCreate云功能事件参数未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试按照Firebase的文档和其他SO帖子来访问已成功部署的云功能的参数值.

I have tried following Firebase's documentation and other SO posts to access a parameter value for a cloud function I've successfully deployed.

不幸的是,我仍然收到

类型错误:无法读取未定义的属性"id"

Type Error: cannot read property 'id' of undefined

我已经记录了event.params,并且它的输出为undefined,所以我理解了这个问题,但是不确定在语法上我应该如何推导出param值.

I've logged event.params and it is outputting as undefined, so I understand the issue, but am unsure how, syntactically, I'm supposed to derive the param value.

下面是我的js代码供参考:

Below is my js code for reference:

exports.observeCreate = functions.firestore.document('/pathOne/{id}/pathTwo/{anotherId}').onCreate(event => {
  console.log(event.params);

  //event prints out data but params undefined...
  const data = event.data()

  var id = event.params.id;

  return admin.firestore().collection('path').doc(id).get().then(doc => {
    const data = doc.data();
    var fcmToken = data.fcmToken;

    var message = {
      notification: {
        title: "x",
        body: "x"
      },
      token: fcmToken
    };

    admin.messaging().send(message)
      .then((response) => {
        console.log('Successfully sent message:', response);
        return;
      })
      .catch((error) => {
        console.log('Error sending message:', error);
        return;
      });

      return;
  })
})

推荐答案

您正在使用Firebase-functions模块的1.0之前的API,但已安装的原始版本是1.0或更高版本.API在1.0中进行了更改.在此处了解有关更改.

You're using the pre-1.0 API for the firebase-functions module, but the acutal version of it you have installed is 1.0 or later. The API changed in 1.0. Read about the changes here.

Firestore(和其他类型的)触发器现在采用类型为 EventContext的第二个参数.它具有一个名为 params 的属性,该属性包含过去在event.params中的数据.

Firestore (and other types of) triggers now take a second parameter of type EventContext. This has a property called params that contains the data that used to be in event.params.

exports.observeCreate = functions.firestore.document('/pathOne/{id}/pathTwo/{anotherId}').onCreate((snapshot, context) => {
  console.log(context.params);
  console.log(context.params.id);
});

也请阅读文档以获取最新信息.关于Firestore触发器.

Please also read the documentation for the most up-to-date information about Firestore triggers.

这篇关于FIrebase Firestore onCreate云功能事件参数未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆