Reputation: 665
I am learning about Firebase by building a to-do application, and for adding items to a certain list, I'd like to find the list by its slug, and add an item to it, provided by the user. Currently, I have the following query:
database.ref('lists').orderByChild('slug').equalTo(listSlug).child('items').push({
title: values.itemTitle
})
This produces the following error:
TypeError: __WEBPACK_IMPORTED_MODULE_1__utils_firebase__.a.ref(...).equalTo(...).child is not a function
The parameters are correct, listSlug
is what it is supposed to be, and values
are what is supposed to be as well. Querying by finding the item with the given slug has worked for me when retrieving the list, but I can't seem to find how to add items to it.
EDIT:
Upon request, here is the database and an unsaved example of what I'd like to add:
Upvotes: 0
Views: 2902
Reputation: 6926
Saving data to the database using push()
, set()
or update()
are methods of the Reference
interface, which also provides further methods to retrieve data from the database.
Now, when calling orderByChild()
or equalTo()
methods on a Reference
, a Query
is returned instead. Unlike a Reference
, a Query
can only be used to retrieve data from the database.
In order to push data to the database, you will need to know the exact location of the array node within the database first, and create a Reference
to it using database.ref(...).child(...)
.
For example, if the array is located at /lists/listId1/items
, you'd need to use:
database.ref('lists').child('listId1').child('items').push({
title: values.itemTitle
})
Or, simply:
database.ref('lists/listId1/items').push({
title: values.itemTitle
})
If you don't know the location of the node, you would need to query the database first to locate it. Or, if you have previously loaded data using a query, you can obtain the Reference
from the results by using DataSnapshot#ref
(or just DataSnapshot#key
for the key), thereby negating the need to query for the location again before pushing.
Upvotes: 3