在Firebase函数中访问数据库值

时间:2019-03-20 22:03:09

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

您好,我正在尝试通过类似这样的函数访问我的Firebase实时数据库

exports.sendNotification = functions.database.ref('/requests/{id}/{formatted_address}').onCreate(event => {
const getSomethingPromise = admin.database().ref('/requests/{id}/{formatted_address}').once('value');
getSomethingPromise.then(results => {
    const somethingSnapshot = results[0];
    console.log(somethingSnapshot);
    // Do something with the snapshot
})
const payLoad = {
    notification:{
        title: 'Message received',
        body: 'You received a new message',
        sound: "default"
    }
};

const options = {
    priority: "high",
    timeToLive: 60*60*2
};

return admin.messaging().sendToTopic("/topics/notifications", payLoad, options);

});

我的最终目标是,每次添加新请求时,都会以/topics/notifications的形式向用户发送一条带有formatted_address的通知,到目前为止,我在打印somethingSnapshot时得到的都是undefined。如何实现?

更新:现在,运行此功能时,我的firebase功能日志中出现错误TypeError: Cannot read property 'match' of undefined

0 个答案:

没有答案