gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:07:03 26 4
gpt4 key购买 nike

我在我的应用程序中实现了 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 触发器的签名已更改。您似乎正在使用测试版,但正在部署到最新版本。请参阅migration guide获取完整说明。

从那里:

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

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

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

}
});

关于node.js - 显示错误的 firebase 函数无法读取未定义的先前属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51782831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com