Node.js 显示错误的firebase函数无法读取未定义的前一个属性
我在我的应用程序中实现了firebase功能,以前它工作正常,但现在它显示错误无法读取未定义的属性“previous” 函数的错误日志Node.js 显示错误的firebase函数无法读取未定义的前一个属性,node.js,firebase,google-cloud-functions,Node.js,Firebase,Google Cloud Functions,我在我的应用程序中实现了firebase功能,以前它工作正常,但现在它显示错误无法读取未定义的属性“previous” 函数的错误日志 TypeError: Cannot read property 'previous' of undefined at exports.LoveNotification.functions.database.ref.onWrite (/user_code/index.js:223:16) at cloudFunctionNewSignature (/user_co
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)
云函数触发器的签名已更改。您似乎正在使用测试版,但正在部署到最新版本。有关完整的说明,请参阅 从那里: 之前(=v1.0.0) 因此,您的代码应该如下所示:
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,那么Firebase的云函数并不是学习它的最佳方式。我建议您先阅读和/或参加考试。它们涵盖了许多基本的JavaScript、Web和Firebase交互。您还可以在local Node.js进程中使用Admin SDK,该进程可以使用本地调试器进行调试。在这些之后,您将更好地为云函数编写代码。
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);
}
});