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

Chr*_*ris 4 firebase google-cloud-functions google-cloud-firestore

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

不幸的是我还在收到一个

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

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

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

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;
  })
})
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 8

您正在为firebase-functions模块使用1.0之前的API,但是您安装的实际版本是1.0或更高版本.API在1.0中更改. 在这里阅读有关更改.

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

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

另请阅读文档以获取有关Firestore触发器的最新信息.