Hendry Lim
Hendry Lim

Reputation: 1974

Pushing Data into Firebase Database without ID

I intend to structure my firebase database as follow.... there is a one user to many book relationships

test-a1dd
   - users
       - c7ffe6dassfasdfadsf
          - name
          - age
          - school
          - books
             - fasdfhgajhdjhfkjadhg
                - title: 'Yeah'

However, when i push data into firebase in the method as shown below, i get an extra id which is expected since I'm pushing in as an object

export const addUserInfo = ({ name, description }) => {
    return dispatch => {
        try {
            const { currentUser } = firebase.auth();
            firebase.database().ref(`/users/${currentUser.uid}`)
                .push({ name, description })
                .then(() => {
                    console.log('User Details Updated')
                })
        } catch (error) {
            console.log('Update User Details Error', error)
        }
    }
}

Any experienced firebase folks can share with him how to push data onto firebase to achieve the firebase structure I desire?

Alternatively, if the firebase data structure I desire is not possible, how can I separate the object 'name, school and age' from the object 'books' so I can display in my list accordingly?

Thanks in advance for your help

Upvotes: 1

Views: 2131

Answers (1)

sketchthat
sketchthat

Reputation: 2688

When you push it generates a key for you. If you use set or update it writes the object as you supplied.

firebase.database().ref(`/users/${currentUser.uid}`)
  .set({ name, description })
  .then(() => {
    return firebase.database().ref(`/users/${currentUser.uid}/books`)
      .push({
        title: 'Yeah'
      });
  });

Upvotes: 1

Related Questions