Firebase 在数据库上使用事务是在给定路径上创建和快速删除数据

Firebase 在数据库上使用事务是在给定路径上创建和快速删除数据,firebase,firebase-realtime-database,Firebase,Firebase Realtime Database,我一直在学习,现在已经掌握了firebase一段时间了 我正在构建一个博客帖子webapp(在官方firebase示例中提供)作为学习示例 我还想添加自定义通知系统,以便用户可以在登录时查看通知 每当用户喜欢或评论博客文章时,都会在文章的作者uid节点下创建一个数据路径,如下所示: --user-notifications ----uid1 --------postid ------------notif: 'notification text' ------------postId:

我一直在学习,现在已经掌握了firebase一段时间了

我正在构建一个博客帖子webapp(在官方firebase示例中提供)作为学习示例

我还想添加自定义通知系统,以便用户可以在登录时查看通知

每当用户喜欢或评论博客文章时,都会在文章的作者uid节点下创建一个数据路径,如下所示:

 --user-notifications
 ----uid1
 --------postid
 ------------notif: 'notification text'
 ------------postId: postId
 ------------viewed: false
 ------------createdAt: 'some date'
 ----uid2
 --------postid
 ------------notif: 'notification text'
 ------------postId: postId
 ------------viewed: false
 ------------createdAt: 'some date'
 var sendCustomNotification = function(fromUID, recieverUID, userName, actionText, postID){
    const ref = firebase.database().ref("user-notifications/" + recieverUID + "/" + postID);
     if(fromUID !== recieverUID){
         ref.transaction(function(obj){
             if(obj){
                 //if notification is not viewd by the user 
                 if(obj.viewed){
                     obj.notif = userName + actionText;
                     obj.viewed = false; 
                 }else{
                 //if notification is viewd by the user the the user's names gets added into one
                     obj.notif = userName + ' and more ' + actionText;
                 }
             }else{
                ref.set({
                    notif: userName +  '  '' + actionText,
                    statusId: postID,
                    viewed: false,
                    createdAt: formatDate(new Date())
                }); 
             }
             return obj;
         })
     }
 }; 
我不想为每个喜欢或评论发送单独的通知,而是想实现facebook的通知类型,即某个帖子的所有喜欢都在一个通知中。例如:约翰、凯特和其他人喜欢你的帖子,或者简、托德和其他人对你的帖子发表了评论

对于上述场景,我使用的事务如下:

 --user-notifications
 ----uid1
 --------postid
 ------------notif: 'notification text'
 ------------postId: postId
 ------------viewed: false
 ------------createdAt: 'some date'
 ----uid2
 --------postid
 ------------notif: 'notification text'
 ------------postId: postId
 ------------viewed: false
 ------------createdAt: 'some date'
 var sendCustomNotification = function(fromUID, recieverUID, userName, actionText, postID){
    const ref = firebase.database().ref("user-notifications/" + recieverUID + "/" + postID);
     if(fromUID !== recieverUID){
         ref.transaction(function(obj){
             if(obj){
                 //if notification is not viewd by the user 
                 if(obj.viewed){
                     obj.notif = userName + actionText;
                     obj.viewed = false; 
                 }else{
                 //if notification is viewd by the user the the user's names gets added into one
                     obj.notif = userName + ' and more ' + actionText;
                 }
             }else{
                ref.set({
                    notif: userName +  '  '' + actionText,
                    statusId: postID,
                    viewed: false,
                    createdAt: formatDate(new Date())
                }); 
             }
             return obj;
         })
     }
 }; 
我在用户评论或喜欢帖子的代码中使用了这个
sendCustomNotification()

现在的问题是:

当用户评论或喜欢调用
sendCustomNotification()
时,在实时数据库中会按预期创建或更新一个节点,但它会立即被删除


不知道这种行为的原因是什么

看起来您根本没有正确调用事务。考虑使用一个非常简单的事务处理程序来学习它们是如何工作的。你不需要云函数来实现这一点——你可以编写一个本地节点程序来简化它@DoughStevenson ya我知道基本事务是如何工作的,我正在使用它们来更新博客的星数post@DougStevenson在上面的事务中,我做错了什么?事务方法有三个参数。据我所知,你通过了一个论点。此外,通常不在transactionUpdate回调中调用set(),您只需返回该位置的数据。@DougStevenson如果还不存在节点,我将使用set方法创建一个节点……好的,您能否提供一些建议,说明如何完成我的用例?看起来您根本没有正确调用事务。考虑使用一个非常简单的事务处理程序来学习它们是如何工作的。你不需要云函数来实现这一点——你可以编写一个本地节点程序来简化它@DoughStevenson ya我知道基本事务是如何工作的,我正在使用它们来更新博客的星数post@DougStevenson在上面的事务中,我做错了什么?事务方法有三个参数。据我所知,你通过了一个论点。另外,您通常不会在transactionUpdate回调中调用set(),您只需返回该位置的数据。@DougStevenson如果还不存在节点,我将使用set方法创建节点……好的,您能提供一些关于如何完成用例的建议吗