How to add another child to my firebase collection?

Viewed 68

So i am trying to find a user with the UserID of with the same value as the variable UserIDand once found I want to add a new child to my collection. I am unsure how to do this, I have tried using

var UserID = 123;

var userEvent  = firebase.database().ref('/users').orderByChild("id").equalTo(UserID).child('Events');

 var eventsData = {
    testdata: 99
 };

 
 userEvent.push(eventsData);

I have tried the above however I am getting the following error message.

Unhandled Rejection (TypeError): firebase_app__WEBPACK_IMPORTED_MODULE_22__.default.database(...).ref(...).orderByChild(...).equalTo(...).child is not a function

Is there a specific way I could do this?

Below is how many users collection looks like currently

enter image description here

1 Answers

The error is pointing out that something is wrong with the ref. I think the issue is that EqualTo() returns an array or object, but I'm not 100% sure.

I would reorganize the code to this:

var userEvent  = firebase.database().ref('/users/' + UserId).child('Events');

You should be able to push to that ref.

Related