Javascript ';onCreate';firebase云函数错误
我正在通过firebase云功能在android上开发推送通知。当我使用Javascript ';onCreate';firebase云函数错误,javascript,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,我正在通过firebase云功能在android上开发推送通知。当我使用onWrite()条件时,我的代码运行得非常好,我试图实现这个评论功能,但在这种情况下,当有人编辑或喜欢评论时,它会生成一个通知,所以我将其更改为onCreate()但现在我遇到一个错误TypeError:无法读取未定义的属性“val” 给你 exports.pushNotificationCommentsPost = functions.database.ref('/post-comments/{postId}/{comm
onWrite()
条件时,我的代码运行得非常好,我试图实现这个评论功能,但在这种情况下,当有人编辑或喜欢评论时,它会生成一个通知,所以我将其更改为onCreate()
但现在我遇到一个错误TypeError:无法读取未定义的属性“val”
给你
exports.pushNotificationCommentsPost = functions.database.ref('/post-comments/{postId}/{commentId}').onCreate((change, context) => {
const commentId = context.params.commentId;
const postId = context.params.postId;
const comment = change.after.val();
const posType = "Post";
const getPostTask = admin.database().ref(`/posts/${postId}`).once('value');
return getPostTask.then(post => {
// some code
})
});
我认为
const comment=change.after.val()中存在问题代码>但我无法理解。您需要更改此选项:
exports.pushNotificationCommentsPost = functions.database.ref('/post-comments/{postId}/{commentId}').onCreate((change, context) => {
为此:
exports.pushNotificationCommentsPost = functions.database.ref('/post-comments/{postId}/{commentId}').onWrite((change, context) => {
工作,因为在实时数据库中创建、更新或删除数据时会触发onWrite
。因此,您可以检索更改前的数据,以及更改后的数据
onCreate()
在实时数据库中创建新数据时触发。因此,您只能检索新添加的数据,例如:
exports.dbCreate = functions.database.ref('/path').onCreate((snap, context) => {
const createdData = snap.val(); // data that was created
});
更多信息请点击此处:
在您的情况下,将其更改为:
exports.pushNotificationCommentsPost = functions.database.ref('/post-comments/{postId}/{commentId}').onCreate((snap, context) => {
const commentId = context.params.commentId;
const postId = context.params.postId;
const comment = snap.val();
const posType = "Post";
});