当用户在我的应用程序中收到好友请求时,我正试图通过firebase云消息向他们发送通知,我将其部署到我的firebase功能中
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notifications_id}').onWrite(event => {
const user_id = event.params.user_id;
const notifications = event.params.notifications;
console.log('This User Id is :', user_id);
});
但是当它试图发送通知时,我在我的函数日志中得到了以下错误
sendNotification
TypeError: Cannot read property 'user_id' of undefined
at exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:8:30)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
at /var/tmp/worker/worker.js:758:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
您必须添加上下文并从中获取参数。尝试:
..'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database
.ref('/Notifications/{user_id}/{notifications_id}')
.onWrite((event, context) => {
const user_id = context.params.user_id;
const notifications = context.params.notifications;
console.log('This User Id is :', user_id);
});