[英]'onCreate' firebase cloud function error
我正在通過 firebase 雲功能在 android 上處理推送通知。 當我使用onWrite()
條件時,我的代碼工作得非常好,我正在嘗試實現此功能進行評論,但在這種情況下,當有人編輯或喜歡評論時,它會產生通知,所以我將其更改為onCreate()
但現在我收到錯誤TypeError: Cannot read property 'val' of undefined
。
這里是..
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
在實時數據庫中創建、更新或刪除數據時觸發。 因此,你能夠檢索數據before
和after
它改變了。
onCreate()
在實時數據庫中創建新數據時觸發。 因此您只能檢索新添加的數據,例如:
exports.dbCreate = functions.database.ref('/path').onCreate((snap, context) => {
const createdData = snap.val(); // data that was created
});
更多信息在這里:
https://firebase.google.com/docs/functions/beta-v1-diff#realtime-database
在您的情況下,將其更改為:
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";
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.