Prevent set() function to overwrite existing data in Firebase

Use update() function instead of set() to prevent overwriting data in Firebase. Example:

javascript

// First instruction
firebase.database().ref('/users/user12345').set({
    
    name: 'Erik',
    age: 29,
    height: '177 cm'

});

// Add weight without overwriting data
firebase.database().ref('/users/user12345').update({
    
    weight: '68 kg'

});


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Output in Firebase
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/* user12345 = {
    
    name: 'Erik',
    age: 29,
    height: '177 cm'
    weight: '68 kg'
    
}*/

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.