Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating nested objects firebase

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.

like image 914
Rohan Dalvi Avatar asked Nov 18 '15 15:11

Rohan Dalvi


People also ask

How to update nested object in Firebase?

To update nested object / map / dictionary in firebase database, you can use Firestore. Encoder to class / struct that is Codable . Calling Firebase: import FirebaseFirestore import FirebaseFirestoreSwift let firestoreEncoder = Firestore.

How do I update nested objects?

To update nested properties in a state object in React: Pass a function to setState to get access to the current state object. Use the spread syntax (...) to create a shallow copy of the object and the nested properties. Override the properties you need to update.

How can you specify what place to update in Firebase realtime database?

Using setValue() in this way overwrites data at the specified location, including any child nodes. However, you can still update a child without rewriting the entire object. If you want to allow users to update their profiles you could update the username as follows: FirebaseDatabase database = FirebaseDatabase.

How do I write data to my Firebase Realtime Database?

This document covers the four methods for writing data to your Firebase Realtime Database: set, update, push, and transactions support. Add to a list of data in the database. Every time you push a new node onto a list, your database generates a unique key, like messages/users/<unique-user-id>/<username>

How to add multiple children to the same Firebase post?

To solve this, the Firebase clients provide a push () function that generates a unique key for each new child. By using unique child keys, several clients can add children to the same location at the same time without worrying about write conflicts. // ... // Post is a json-serializable type.

How does the unique key work in Firebase?

The unique key is based on a timestamp, so list items will automatically be ordered chronologically. Because Firebase generates a unique key for each blog post, no write conflicts will occur if multiple users add a post at the same time. Your database data now looks like this:

What is the use of child key in Firebase?

To solve this, the Firebase clients provide a push() function that generates a unique key for each new child. By using unique child keys, several clients can add children to the same location at the same time without worrying about write conflicts.


2 Answers

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.

like image 79
David East Avatar answered Oct 16 '22 20:10

David East


This question provides a more recent solution that works with cloud firestore.

Rather than using "/", one may use "." instead:

var userRef = ref.child(hashedEmail);
var updateObject = {
   name: 'Rohan Dalvi', 
   "externalLinks.website": 'mywebsite'
};
userRef.update(updateObject);
like image 31
D W Avatar answered Oct 16 '22 20:10

D W