FabricioG
FabricioG

Reputation: 3320

How to eliminate extra uid from appearing in firebase push

I currently have the following push query for my firebase database.

setVisitorAttendanceDate = (orgId, attendeeUid, date) => this.db.ref(`organization/${orgId}/visitor-attendance/${date}`).push({[attendeeUid]: true});

This creates the following:

- visitor-attendance
- 2020-11-30-PM
-MNSIxmbzLwlW5Dq83Ws
-MNSIxm_woAnBAkVQWRV: true

It adds the date as expected but it adds an extra UID.

Right after the date it adds this uid for the object:

-MNSIxmbzLwlW5Dq83Ws

How can I make it to be with out that UID? So this

- visitor-attendance
- 2020-11-30-PM
-MNSIxm_woAnBAkVQWRV: true

Upvotes: 0

Views: 39

Answers (2)

FabricioG
FabricioG

Reputation: 3320

The above comment and answer were very helpful. I ended up solving this by changing push to update.

The code now reads:

setVisitorAttendanceDate = (orgId, attendeeUid, date) => this.db.ref(`organization/${orgId}/visitor-attendance/${date}`).update({[attendeeUid]: true});

Upvotes: 0

Jay
Jay

Reputation: 35659

Remember, everything in the Realtime Database are key: value pairs. When a path is defined and you .push in that path, a node with a child key: value pair is created that sits 'under' that path.

If you look at the code, you're actually defining a path to the data with the final last component being date

this.db.ref(`organization/${orgId}/visitor-attendance/${date}`).push({[attendeeUid]: true});
                                                         ^^^ path          ^^^ key   ^^^ value

with the path being

organization
   orgId
      visitor-attendance
         date
           the pushID
               MNSIxm_woAnBAkVQWRV: true

From the Firebase Documentation

The push() method generates a unique key every time a new child is added to the specified Firebase reference.

One possible fix is to use .set as shown in the Guide

For basic write operations, you can use set() to save data to a specified reference, replacing any existing data at that path.

firebase.database().ref('users/' + userId).set({
    username: name,
    email: email,
    profile_picture : imageUrl
  });

Upvotes: 1

Related Questions