Вам нужно изменить это:
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";
});