firebase functions showing error cannot read property previous of undefined

人盡茶涼 提交于 2020-06-23 04:28:09

问题


I had implemented firebase functions in my app and previously it was working fine but now it is showing error Cannot read property 'previous' of undefined

Error Logs of function

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)

回答1:


The signature of Cloud Functions triggers has changed. You seem to be using beta, but are deploying to the latest version. See the migration guide for complete instructions.

From there:

Before (<= v0.9.1)

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

Now (>= 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
});

So your code should look something like this:

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

    }
});


来源:https://stackoverflow.com/questions/51782831/firebase-functions-showing-error-cannot-read-property-previous-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!