【问题标题】:firebase functions showing error cannot read property previous of undefined显示错误的firebase函数无法读取未定义的先前属性
【发布时间】:2019-01-17 20:27:26
【问题描述】:

我已经在我的应用程序中实现了 firebase 功能,以前它工作正常,但现在显示错误 Cannot read property 'previous' of undefined

函数错误日志

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)

【问题讨论】:

    标签: node.js firebase google-cloud-functions


    【解决方案1】:

    Cloud Functions 触发器的签名已更改。您似乎正在使用测试版,但正在部署到最新版本。有关完整说明,请参阅migration guide

    从那里:

    之前(

    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);
    
        }
    });
    

    【讨论】:

    • 你能帮我更新版本吗,因为我尝试了一些小改动,但没有成功。 .你能发布我的代码的更新代码吗?我试过但搞砸了。
    • 我添加了一个快速版本。但如果您是 JavaScript 新手,Cloud Functions for Firebase 并不是学习它的最佳方式。我建议先阅读documentation for Web developers 和/或阅读codelab for Web developer。它们涵盖了许多基本的 JavaScript、Web 和 Firebase 交互。您还可以在本地 Node.js 进程中使用 Admin SDK,该进程可以使用本地调试器进行调试。完成这些之后,您将能够更好地为 Cloud Functions 编写代码。
    猜你喜欢
    • 2018-12-09
    • 2020-08-01
    • 2019-04-05
    • 2018-10-30
    • 2018-04-01
    • 2020-03-26
    • 2018-09-13
    相关资源
    最近更新 更多