How to request multiple values โ€‹โ€‹for a given key in firebase

I tried using the 'OR' || operator in the equalTo() method, but that doesn't seem to work. Should I make a separate call for each value or is there a way to make conditional requests 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)); }); }; }; 
+5
source share
2 answers

What you typed there was a logical OR that JavaScript interprets a programming language. Expression Result:

 'one-act-play' || 'ten-min-play' || 'full-length-play' 

will be simple:

 'one-act-play' 

So, Firebase Realtime Database will not see what you are trying to say.

The Firebase Realtime database does not have a query type concept that has an OR. You will need to request each type of thing separately and combine them together in your code as needed.

+2
source

There is no concept of doing what you ask. You can make a rotation in which you can get values โ€‹โ€‹based on one one-act-play filter using equalTo and the rest, which you can filter at the end of the user, for example:

 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 a library created to fulfill such a Querybase requirement

+1
source

Source: https://habr.com/ru/post/1274674/


All Articles