node.js - 使用 Cloud Functions for Firebase 的无服务器通知
问题描述
我使用了 firebase 聊天通知云功能,但是当通知触发时,我在 firebase 功能控制台中收到此错误
无法读取未定义的属性“当前”
在exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:8:35)
这是我的功能:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/notifications/messages/{pushId}').onWrite(event => {
console.log('Push notification event triggered for testing');
console.log(event);
const message = event.data.current.val();
const senderUid = message.from;
const receiverUid = message.to;
console.log(receiverUid);
const promises = [];
if (senderUid === receiverUid) {
//if sender is receiver, don't send notification
promises.push(event.data.current.ref.remove());
return Promise.all(promises);
}
const getInstanceIdPromise = admin.database().ref(`/usersnew/${receiverUid}`).once('value');
const getSenderUidPromise = admin.auth().getUser(senderUid);
return Promise.all([getInstanceIdPromise, getSenderUidPromise]).then(results => {
const instanceId = results[0].val();
const sender = results[1];
console.log('notifying ' + receiverUid + ' about ' + message.body + ' from ' + senderUid);
const payload = {
notification: {
title: sender.displayName,
body: message.body,
icon: sender.photoURL
}
};
admin.messaging().sendToDevice(instanceId, payload)
.then(function (response) {
return console.log("Successfully sent message:", response);
})
.catch(function (error) {
console.log("Error sending message:", error);
});
return console.log('This is the notify feature');
});
});
有谁知道如何解决这个问题?当我记录事件时,它在控制台中显示如下
{ before:
DataSnapshot {
app:
FirebaseApp {
firebaseInternals_: [Object],
services_: {},
isDeleted_: false,
name_: '__admin__',
options_: [Object],
INTERNAL: [Object] },
instance: 'https://teleport-24f52.firebaseio.com',
_path: '/notifications/messages/-LIlFNd2spo_V1rM-G-f',
_data: null },
after:
DataSnapshot {
app:
FirebaseApp {
firebaseInternals_: [Object],
services_: {},
isDeleted_: false,
name_: '__admin__',
options_: [Object],
INTERNAL: [Object] },
instance: 'https://teleport-24f52.firebaseio.com',
_path: '/notifications/messages/-LIlFNd2spo_V1rM-G-f',
_data:
{ body: 'abc',
dayTimestamp: 1532975400000,
from: 'q8gtwtwXqbV2DtpsrbYajFsWzSr2',
negatedTimestamp: -1533056068309,
timestamp: 1533056068309,
to: 'Cmpu7mbIENTYyoHZjCjZnbnBMbl2' } } }
10:22:44.625 PM
解决方案
在您的代码中,event.data.current
应该是event.after.val()
,event.after.ref
等...
云函数 1.0 中的 API 发生了变化。
阅读: https ://firebase.google.com/docs/functions/database-events#reading_the_previous_value https://firebase.google.com/docs/functions/beta-v1-diff
推荐阅读
- c++ - Visual Studio 2015 构建工具/CPP 运行时 v140 Dockerfile 无法正常工作
- python - 解码 Base64 字符串时填充不正确
- flutter - 如何在颤振中使用 RevenueCat 取消订阅?
- sql - 使用 3 个表优化 SQL 查询
- yii2 - Yii2 gridview按钮问题(可能在pjax之后)
- javascript - 仅使列表中单击的项目在反应中处于活动状态
- javascript - React Hook 表单 - 控制器 - React AsyncSelect - Lodash Debounce | 未能显示 loadOptions
- reactjs - 反应,过期 JWT 令牌后,移动到登录页面
- heroku - 使用 Heroku 时,我是否需要支付第二个爱好 dyno 来运行工作进程?
- java - 将 JPanel 添加到 JFrame