引用错误事件未定义 node.js

reference error event is not defined node.js

我正在使用 Notifications,但 Notification 不工作并收到此错误,我是新手,所以我无法完全理解这一点并被错误弄得一团糟。任何帮助将不胜感激,我没有找到任何相关的解决方案

ReferenceError: event is not defined
at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:9:31)
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:744:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

这是我的 index.js

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

exports.sendNotification = 

 functions.database.ref('/notifications/{user_id}/{notification_id}')
.onWrite((change, context) => {

    const user_id = event.params.user_id;
    const notification = event.params.notification;

    console.log('We have a notification to send to: ', user_id);

        if(!event.data.val())
        {
            return console.log('A Notification has been deleted from database: ', notification_id);
        }

        const fromUser = admin.database().ref(`/notifications/${user_id}/${notification_id}`);
        return fromUser.then(fromUserResult => {

            const from_user_id = fromUserResult.val().from;

            console.log('You have new Notification from: ', from_user_id);

            const userQuery = admin.database().ref(`Users/${from_user_id}/name`).once('value');
            const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

            return Promise.all([userQuery, deviceToken]).then(result => {


                const userName = result[0].val();
                const token_id = result[1].val();

                    const payload = {
                      notification: {
                        title: "Message Request",
                        body: `${userName} has sent you a Message Request`,
                        icon: "logo.png",
                        click_action: "com.example.gab.quadrantms_TARGET_NOTIFICATION"
                    },
                    data : {
                        from_user_id : from_user_id
                    }
                };

                return admin.messaging().sendtoDevice(token_id, payload).then(response =>{

                    console.log('This was the Notification Feature');
                    return true;
                });
            });
        });
    });

正如错误所说,event 未在任何地方定义。看起来您应该改用 context

const user_id = context.params.user_id;
const notification = context.params.notification;

以下是 EventContext 的文档:https://firebase.google.com/docs/reference/functions/functions.EventContext

RefBuilder.onWritehttps://firebase.google.com/docs/reference/functions/functions.database.RefBuilder#onWrite