Issue
From the Firebase note:
Given a single key path like alanisawesome
, updateChildren()
only updates data at the first child level, and any data passed in beyond the first child level is a treated as a setValue()
operation. Multi-path behavior allows longer paths (like alanisawesome/nickname
) to be used without overwriting data. This is why the first example differs from the second example.
I am trying to use a single function createOrUpdateData(object)
in my code. In case of update, it updates first level children properly, but if I have nested object passed, then it deletes all other properties of that nested object.
Here's 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 pass:
{
name: 'Rohan Dalvi',
externalLinks: {
website: 'mywebsite'
}
}
Then it will delete other properties inside externalLinks object. Is there a cleaner and simpler way of avoiding this?
In short, how do I make sure nested objects are only updated and that data is not deleted.
Solution
You can use multi-path updates.
var userRef = ref.child(hashedEmail);
var updateObject = {
name: 'Rohan Dalvi',
"externalLinks/website": 'mywebsite'
};
userRef.update(updateObject);
By using the "externalLinks/website"
syntax in the object literal it will treat the nested path as an update and not a set for the nested object. This keeps nested data from being deleted.
Answered By - David East
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.