firebase functions showing error cannot read property previous of undefined

后端 未结 1 1154
忘掉有多难
忘掉有多难 2021-01-15 18:30

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

1条回答
  •  爱一瞬间的悲伤
    2021-01-15 19:09

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

    0 讨论(0)
提交回复
热议问题