0

I have a firebase realtime database and this this the structure of it:

enter image description here

you can see there is only one user with "hgjfj" userName and I want to add another users in same queue object of each barber using react native

this is the function of adding data:

NewBooking() {
        var queue = { userName: 'gfdgdfg', userPhone: 54535 }
      firebase.database().ref('Barbers/').push({
        queue: queue
      }).then((data) => {
        //success callback
        console.log('data ', data)
      }).catch((error) => {
        //error callback
        console.log('error ', error)
      })
  }

is there a way to get the generated id by firebase like get the LWC312750NY35f3YKEL and push new user on the queue of that id

1 Answer 1

3

Your current queue is not a queue at all, but a single object with a single pair of properties. If you want to store a list of objects for each barber, then queue should be a collection.


To add an such an object to each barber's queue, you will first need to read all barbers, then loop over the results, and finally add the object to each queue.

Something like this:

var newItem = { userName: 'gfdgdfg', userPhone: 54535 }
firebase.database().ref('Barbers').once('value').then(function(snapshot) {
  snapshot.forEach(function(barberSnapshot) {
    barberSnapshot.child('queue').ref.push(newItem);
  });
});

If you know the ID of the barber whose queue you want to add it to, the code can be a lot simpler:

var newItem = { userName: 'gfdgdfg', userPhone: 54535 }
firebase.database().ref('Barbers/UIDofBarber/queue').push(newItem);

If you want to clear out the queue for each barber, to get rid of the faulty object you have in there now:

firebase.database().ref('Barbers').once('value').then(function(snapshot) {
  snapshot.forEach(function(barberSnapshot) {
    barberSnapshot.child('queue').ref.remove();
  });
});

By the way, you're violating one of the recommendations from the Firebase documentation, which is to keep your data structure flat. Right now the code above needs to read both the metadata about each barber and the queue, while it really only needs to access the queue. I recommend creating to top-level collections: one for the metadata about each barber, and one for their queue:

barbers
  barberUID1: { ... }
  barberUID2: { ... }
queues
  barberUID1: {
    queuePushID11: { ... }
    queuePushID12: { ... }
  }
  barberUID2: {
    queuePushID21: { ... }
    queuePushID22: { ... }
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for amazing and clear explanation but I have question about the UIDofBarber how can I get it so I can know the ID and use the simple code rather than the long one? and when you say UID do you mean the "LWC312750NY35f3YKEL" for example as you can see in image of database or you mean another id that I should add as new single object for each barber like when I add name and rating for them? @Frank van Puffelen
I said ID nor UID, because the keys you use for barbers are generated by calling push(). You'd typically know it because it's something that the user clicked on. If your barbers are actual users of the app, you should use Firebase Authentication to sign them in and then use their UID for the keys, instead of using push() for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.