gpt4 book ai didi

javascript - 如何正确地退出一个函数?

转载 作者:行者123 更新时间:2023-11-28 20:52:03 26 4
gpt4 key购买 nike

所以我的推送通知工作得很好。我遇到的一个大问题是,如果用户对自己的照片点赞/发表评论,他们将收到自己的推送通知。我不知道如何解决这个问题。例如,假设我喜欢我自己的照片,我会收到推送通知“克林特喜欢你的帖子”,这就是我,如果我正在回复对我的帖子的评论,这与评论相同。 “克林特评论了你的帖子”。

这是我通过 Atom 为通知编写的代码。


exports.observeComments = functions.database.ref('/comments/{postId}/{commentId}').onCreate((snapshot, event) => {
var postId = event.params.postId;
var commentId = event.params.commentId;

return admin.database().ref('/comments/' + postId + '/' + commentId).once('value', snapshot => {
var comment = snapshot.val();
var commentUid = comment.uid;

return admin.database().ref('/users/' + commentUid).once('value', snapshot => {
var commentingUser = snapshot.val();
var username = commentingUser.username;

return admin.database().ref('/posts/' + postId).once('value', snapshot => {
var post = snapshot.val();
var postOwnerUid = post.ownerUid;

return admin.database().ref('/users/' + postOwnerUid).once('value', snapshot => {
var postOwner = snapshot.val();

var payload = {
notification: {
body: username + ' commented on your post '

}
};

admin.messaging().sendToDevice(postOwner.fcmToken, payload)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
return response;
})
.catch((error) => {
console.log('Error sending message:', error);
throw new Error('Error sending message:', error);
});
})
})
})
})
})


exports.observeLikes = functions.database.ref('/user-likes/{uid}/{postId}').onCreate((snapshot, event) => {
var uid = event.params.uid;
var postId = event.params.postId;

return admin.database().ref('/users/' + uid).once('value', snapshot => {
var userThatLikedPost = snapshot.val();

return admin.database().ref('/posts/' + postId).once('value', snapshot => {
var post = snapshot.val();

return admin.database().ref('/users/' + post.ownerUid).once('value', snapshot => {
var postOwner = snapshot.val();

var payload = {
notification: {
body: userThatLikedPost.username + ' liked your post '
}
};

admin.messaging().sendToDevice(postOwner.fcmToken, payload)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
return response;
})
.catch((error) => {
console.log('Error sending message:', error);
throw new Error('Error sending message:', error);
});
});
})
})
})

最佳答案

如果我理解正确,你只需要添加中断条件。只需在

之后添加

var postOwnerUid = post.ownerUid;

这一行:

if(commentUid === postOwnerUid)
return Promise.resolve();

及之后

var post = snapshot.val();

这一行:

if(uid === post.ownerUid)
return Promise.resolve();

关于javascript - 如何正确地退出一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57470564/

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