显示错误的 firebase 函数无法读取未定义的先前属性

2023-12-23

我已经在我的应用程序中实现了 firebase 功能,之前它工作正常,但现在显示错误无法读取未定义的属性“前一个”

函数错误日志

TypeError: Cannot read property 'previous' of undefined
at exports.LoveNotification.functions.database.ref.onWrite (/user_code/index.js:223:16)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:109:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:139:20)
at /var/tmp/worker/worker.js:730:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

Cloud Functions 触发器的签名已更改。您似乎正在使用测试版,但正在部署到最新版本。请参阅迁移指南 https://firebase.google.com/docs/functions/beta-v1-diff以获得完整的说明。

从那里:

之前 (

exports.dbWrite = functions.database.ref('/path').onWrite((event) => {
  const beforeData = event.data.previous.val(); // data before the write
  const afterData = event.data.val(); // data after the write
});

现在(>= v1.0.0)

exports.dbWrite = functions.database.ref('/path').onWrite((change, context) => {
  const beforeData = change.before.val(); // data before the write
  const afterData = change.after.val(); // data after the write
});

所以你的代码应该是这样的:

exports.LoveNotification = functions.database.ref("/Member/{pushId}").onWrite((change, context) => {

 if (change.before.exists()) {
    return;
 } else {
    var eventLove = change.after.data.val();
    var author =eventLove.fullname;
    var title = eventLove.course;
    var key = eventLove.key;

    const payload = {
        "data": {
                    "post_id": key
                  },
        notification: {
            title: author +'Joined the app',
            body: `Course `+title,
            sound: "default",
            icon: "ic_launcher",

        }
    };

    const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24 //24 hours
    };
    console.log('Sending notifications');
    return admin.messaging().sendToTopic("Member", payload, options);

    }
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

显示错误的 firebase 函数无法读取未定义的先前属性 的相关文章

随机推荐