According to the information from the Firebase note:
When using a single key path such as alanisawesome
, the updateChildren()
method will only update data at the first child level. Any data passed in beyond the first child level will be treated as a setValue()
operation. This is where multi-path behavior comes into play, allowing longer paths like alanisawesome/nickname
to be used without overwriting data. This explains the difference between the first and second examples provided.
My goal is to implement a single function called createOrUpdateData(object)
in my code. When updating, it correctly updates first-level children, but if a nested object is passed, it ends up deleting all other properties within that nested object.
Below is the code snippet demonstrating this:
function saveUserDetails(email, object){
var hashedEmail = Utilities.getHashCode(email);
var userRef = ref.child(hashedEmail);
return $q(function(resolve, reject){
userRef.update(object, function(error){
if(error){
reject(error);
}else{
resolve("Updated successfully!");
}
});
});
}
For example, if I pass the following object:
{
name: 'Rohan Dalvi',
externalLinks: {
website: 'mywebsite'
}
}
It will lead to the deletion of other properties within the externalLinks
object. Is there a more efficient and straightforward way to prevent this issue?
In essence, how can I ensure that only nested objects are updated without inadvertently deleting any existing data?