How can I update the nick contained in all the favorite nodes when the user updates their nick

25 Views Asked by At

I have a structure where a user stores their favorite users and in this node some fields like the nick, level and photo are repeated.

When a user modifies his nick I want to modify it in all the favorite nodes of the users that have this as favorite and where the nick is also.

[Structure of nodes in database]1

{
"userProfile": {
  "8459xIX2aQhbRAVvLjCKYrkygG53" : {
    "email" : "[email protected]",
    "fotoPerfil" : "",
    "nick" : "nick4"
  },
  "ZQ65L8W54AW58FqNfXqknEW7O6T2" : {
    "Favoritos" : {
      "m43MatCb9bRQymSKYfWChaSpFPy2" : {
        "id_favorito" : "m43MatCb9bRQymSKYfWChaSpFPy2",
        "nick" : "nick22"
      }
    },
    "email" : "[email protected]",
    "fotoPerfil" : "",
    "nick" : "nick11"
  },
  "m43MatCb9bRQymSKYfWChaSpFPy2" : {
    "Favoritos" : {
      "ZQ65L8W54AW58FqNfXqknEW7O6T2" : {
        "id_favorito" : "ZQ65L8W54AW58FqNfXqknEW7O6T2",
        "nick" : "nick11"
      }
    },
    "email" : "[email protected]",
    "fotoPerfil" : "",
    "nick" : "nick23"
  },
  "toRFGF7CoKfb3UFcJYxOAXgeR9Z2" : {
    "email" : "[email protected]",
    "fotoPerfil" : "",
    "nick" : "nick3"
  }
 }
}

I have the following code but it is not enough to get to the nodes that interest me.

  updateNick_Favoritos(nick) {
    debugger
    var updates = {};
    let ref = firebase.database().ref(`/userProfile/`);
    var query = ref.child('Favoritos').orderByChild('id_favorito').equalTo(this.currentUser.uid);
    query.once('value', function(snapshot) {
      debugger
      snapshot.forEach(favoritoSnapshot => {
        updates['userProfile/' + key_desconocida + '/Favoritos/' + favoritoSnapshot.key + '/nick'] = nick;
        console.log('key:'+favoritoSnapshot.key);
        console.log('key:'+favoritoSnapshot.val());
        return false;
      })
      ref.update(updates);
    });
  }

[Codec]2

What is the correct query to do what I want? And since we are if they tell me how to get the value of the unknown route for the update I would greatly appreciate it.

0

There are 0 best solutions below