I attempted to use an 'OR' ||
statement in the equalTo()
method, but this does not seem to work. Do I have to make a separate call for each value or is there a way to make conditional queries in firebase?
export const startSetContent = () => {
return (dispatch, getState) => {
const ref = database.ref("content");
return ref
.orderByChild("category")
.equalTo('one-act-play' || 'ten-min-play' || 'full-length-play')
.once('value')
.then(snapshot => {
const content = [];
snapshot.forEach(childSnapshot => {
content.push({
id: childSnapshot.key,
...childSnapshot.val()
});
});
dispatch(setContent(content));
});
};
};
public String getKey () Returns. The key name for the source location of this snapshot or null if this snapshot points to the database root.
Firebase is categorized as a NoSQL database program, which stores data in JSON-like documents. In Firebase, a document is a set of key-value pairs defined by a schema.
Firebase orderByChild once child_added is only giving one child. 0. Returning a single child's value on Firebase query using orderByChild and equalTo. 3. unspecified index when searching data with firebase cloud function on nested object running nested Query.
We can filter data in one of three ways: by child key, by key, or by value. A query starts with one of these parameters, and then must be combined with one or more of the following parameters: startAt , endAt , limitToFirst , limitToLast , or equalTo .
What you typed there was a logical OR that JavaScript the programming language interprets. The result of the expression:
'one-act-play' || 'ten-min-play' || 'full-length-play'
is going to be simply:
'one-act-play'
So, Firebase Realtime Database will not see what you're trying to tell it.
Firebase Realtime Database has no concept of a kind of query that has ORs in it. You will have to query each type of thing separately and merge them together in your code as needed.
There is no concept of doing that you are asking for. You can do a turn around in which you can get the values on basis of one filter one-act-play
using equalTo and rest you can filter on user end like:
export const startSetContent = () => {
return (dispatch, getState) => {
const ref = database.ref("content");
return ref
.orderByChild("category")
.equalTo('one-act-play')
.once('value')
.then(snapshot => {
const content = [];
Object.keys(snapshot.val()).map(k => {
if(k == 'ten-min-play' || k == 'full-length-play'){
snapshot.forEach(childSnapshot => {
content.push({
id: childSnapshot.key,
...childSnapshot.val()
});
});
}
dispatch(setContent(content));
});
};
};
Or you can use library made to fulfil this kind of requirement Querybase
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With