【问题标题】:event.params evaluating to undefined; Cant access event.params.post in Firebase Realtime Database Using Cloud Functionsevent.params 评估为未定义;无法使用 Cloud Functions 访问 Firebase 实时数据库中的 event.params.post
【发布时间】:2019-11-13 10:38:23
【问题描述】:

我已经尝试了所有我知道的方法来让这个云函数正常工作,但我不知道这个云函数出于什么原因将 event.params 评估为未定义。

我的云功能是:-

'use-strict'

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('posts/{post}').onWrite(event => {
  const payload = {
    notification: {
      title: "Title",
      body: event.params.post,
      icon: "default"
    }
  };

  return admin.messaging().sendToDevice(payload).then(result => {
    console.log("notification Sent")
  });

});

我得到的错误:-

sendNotification
TypeError: Cannot read property 'post' of undefined at exports.sendNotification.functions.database.ref.onWrite.event(/srv/index.js: 11: 23) at cloudFunction(/srv/node_modules / firebase - functions / lib / cloud - functions.js: 119: 23) at / worker / worker.js: 825: 24 at < anonymous > at process._tickDomainCallback(internal / process / next_tick.js: 229: 7)

我的数据库有一个名为“posts”的直接子级。每当我在其中添加一个孩子时说{post} 函数触发但我无法获得{post} 值。

【问题讨论】:

    标签: android firebase firebase-realtime-database firebase-cloud-messaging google-cloud-functions


    【解决方案1】:

    这很可能是因为您在新版本的 Firebase SDK for Cloud Functions 中使用了旧语法。

    您的语法 (.onWrite(event =&gt; {})) 对应于 SDK 版本

    https://firebase.google.com/docs/functions/beta-v1-diff?authuser=0#realtime-database

    您可以在package.json文件中查看Cloud Function的版本。

    如果确认您的版本是> v1.0.0,您应该如下调整您的代码:

    exports.sendNotification = functions.database.ref('posts/{post}').onWrite((change, context) => {
      const payload = {
        notification: {
          title: "Title",
          body: context.params.post,
          icon: "default"
        }
      };
    
      return admin.messaging().sendToDevice(payload)
      .then(result => {
        console.log("notification Sent");
        return null;
      });
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 2020-02-01
      • 2023-03-11
      • 2018-08-10
      • 1970-01-01
      相关资源
      最近更新 更多