Updating the database of objects of nested objects

From the Firebase note:

For one key path, such as alanisawesome , updateChildren() updates the data only at the first child level, and any data transferred outside the first child level is treated as a setValue() operation. Multi-channel behavior allows you to use longer paths (e.g. alanisawesome/nickname ) without overwriting data. That is why the first example is different from the second example.

I am trying to use one function createOrUpdateData(object) in my code. In the case of an update, it correctly updates the first level child elements, but if I have a nested object, it removes all other properties of this nested object.

Here is the code:

 function saveUserDetails(email,object){ var hashedEmail = Utilities.getHashCode(email); var userRef = ref.child(hashedEmail); return $q(function(resolve,reject){ return userRef.update(object, function(error){ if(error){ reject(error); }else{ resolve("Updated successfully!"); } }); }); } 

So if I go through:

 { name: 'Rohan Dalvi', externalLinks: { website: 'mywebsite' } } 

Then it will remove other properties inside the externalLinks object. Is there a cleaner and easier way to avoid this?

In short, how can I make sure that nested objects are only updated and that the data is not deleted.

+5
source share
1 answer

You can use multi-channel updates .

 var userRef = ref.child(hashedEmail); var updateObject = { name: 'Rohan Dalvi', "externalLinks/website": 'mywebsite' }; userRef.update(updateObject); 

Using the syntax "externalLinks/website" in the object's literature, it will treat the nested path as an update, not a collection for the nested object. This prevents the deletion of embedded data.

+10
source

Source: https://habr.com/ru/post/1236247/


All Articles