删除Firebase云端函数中的节点时,不会调用onDelete [英] onDelete not being called when deleting a node in Cloud Functions for Firebase

查看:113
本文介绍了删除Firebase云端函数中的节点时,不会调用onDelete的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为关注跟随者的Firebase应用程序构建触发器。以下是我的云代码片段。我想在用户关注时增加一个计数器。为此,我使用oncreate(因为当用户获得他们的第一个追随者,因为结构不存在,直到这一点),然后我使用onupdate。然后我使用ondelete递减以下计数,当用户取消关注和被删除。

我遇到的问题是.ondelete没有被调用,只有.onupdate是无论用户被添加或删除(这回想起来是有道理的,我猜)被调用。我的问题是如何编写云功能,以删除添加删除。

数据库看起来像这样

  user1 
- 跟随
-user2
- 少量的user2data
-user3
- 少量的user3data $




exports.countFollowersUpdate = functions.database.ref('/ {pushId} / followers /')
.onUpdate(event => {

console.log countFollowers)


event.data.ref.parent.child('followers_count')。transaction(function(current_value)){

return(current_value | | 0)+ 1;
});


});
$ b $ export.countFollowersCreate = functions.database.ref('/ {pushId} / followers /')
.onCreate(event => {

console。 log(countFollowers)


event.data.ref.parent.child('followers_count')。transaction(function(current_value){

return current_value || 0)+ 1;
});


});


exports.countFollowersDelete = functions.database.ref('/ {pushId} / followers /')
.onDelete(event => {

console.log(countFollowers2)


event.data.ref.parent.child('followers_count')。transaction(function(current_value){

if((current_value - 1)> 0){
return(current_value - 1);
}
else {
return 0;
}




$ div class =h2_lin>解决方案

onDelete 没有被调用,因为你正在监听整个 followers 节点,所以只有当跟随者计数到零你可能希望所有这些更像:

  functions.database.ref('/ {pushId } / followers / {followerId}')。onDelete()

级别的推送id。结构会通常更像 / users / {pushId} / followers / {followerId}


I am trying to build triggers for a firebase app around following and followers. Below is a snippet of my cloud code. I want to increase a counter when a user follows. For this I use oncreate (for when the user gains their first follower due the structure not existing until this point) and then I use onupdate. I then use ondelete to decrement the following count when a user unfollows and is removed.

The problem I am having is that .ondelete is not being called and only .onupdate is being called regardless of users being added or removed (which looking back makes sense I guess). My question is how to write the cloud functions to separate the deletions from the additions.

the database looks something like this

user1
  - following
     -user2
       -small amount of user2data
     -user3
       -small amount of user3data

And the code:

    exports.countFollowersUpdate = functions.database.ref('/{pushId}/followers/')
        .onUpdate(event => {

          console.log("countFollowers")


          event.data.ref.parent.child('followers_count').transaction(function (current_value) {

            return (current_value || 0) + 1;
          });


        });

exports.countFollowersCreate = functions.database.ref('/{pushId}/followers/')
    .onCreate(event => {

      console.log("countFollowers")


      event.data.ref.parent.child('followers_count').transaction(function (current_value) {

        return (current_value || 0) + 1;
      });


    });


exports.countFollowersDelete = functions.database.ref('/{pushId}/followers/')
    .onDelete(event => {

      console.log("countFollowers2")


      event.data.ref.parent.child('followers_count').transaction(function (current_value) {

        if ((current_value - 1) > 0) {
          return (current_value - 1);
        }
        else{
          return 0;
        }
      });

解决方案

onDelete isn't being called because you're listening to the entire followers node, so it would only be called when follower counts goes to zero (nothing left). Instead you probably want all of these to be more like:

functions.database.ref('/{pushId}/followers/{followerId}').onDelete()

It's also unusual that you have a top-level push id. A structure would normally be more like /users/{pushId}/followers/{followerId}.

这篇关于删除Firebase云端函数中的节点时,不会调用onDelete的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆