【问题标题】:"TypeError: Cannot read property 'val' of undefined" for push notification推送通知的“TypeError:无法读取未定义的属性 'val'”
【发布时间】:2020-11-29 03:03:00
【问题描述】:

嗨,我对此很陌生。

Here is my data tree on Firebase:

我正在尝试访问“sightings”下新创建的节点,这是我的云功能代码。

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

exports.announceSighting = functions.database
    .ref('sightings/{sightingId}')
    .onCreate(event => {
        const sighting = event.data.val()
        sendNotification(sighting)
    })

function sendNotification(sighting) {
    const name = sighting.name

    const payload = {
        notification: {
            title: 'New Sighting Available',
            body: 'Hi',
            // body: title + name,
            sound: 'default'
        }
    }
    console.log(payload)

const topic = "Sightings"
admin.messaging().sendToTopic(topic, payload)

}

据我了解,我正在收听 .ref('sightings/{sightingId}') 的新节点 sightingId 是我从教程中解释的,因为父节点的名称是 sightings ,但我不知道我应该在此处放置什么以表明我正在寻找目击者

下的孩子

This is the error I've been getting this:

非常感谢您的帮助。

【问题讨论】:

标签: javascript firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

根据docs,您应该使用快照来获取创建对象的数据,如下所示:

exports.announceSighting = functions.database.ref('/sightings/{sightingId}')
    .onCreate((snapshot, context) => {
      const sighting = snapshot.val();
      sendNotification(sighting)
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-19
    • 2020-04-20
    • 2017-11-23
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2015-07-10
    相关资源
    最近更新 更多