georgewoofbates
georgewoofbates

Reputation: 330

Using equalTo() function in Firebase Database JavaScript v9

I am trying to use the equalTo function in the Firebase Database JavaScript v9 SDK. It's not very well documented like V8 is so can't figure out how to do what I want to do.

I want to filter results by the type field. My parent field is request-log with child elements like below.

{"backtrace":false,"message":false,"organisationID":"Empty","postData":false,"referrer":"Empty","remoteIP":"0.0.0.0","requestMethod":"POST","time":1630129562,"type":"Error","url":"Empty","user":{"email":"Empty","firstName":"George","id":37,"lastName":"Bates"},"userAgent":"Empty"}

I am trying the following, but not getting any results when loaded:

const readNewLogEntries = query(child(ref(db), 'request-log'), equalTo('Error', 'type'));

I have also tried this, as the reference docs say for the 2nd argument in the equalTo() function

This argument is only allowed if ordering by child, value, or priority.

But this isn't working either

const readNewLogEntries = query(child(ref(db), 'request-log'), orderByChild('type'), equalTo('Request', 'type'));

EDIT: Screenshot of DB

Screenshot of DB

Upvotes: 4

Views: 2751

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50830

Remove the second parameter from equalTo().

The optional key argument can be used to further limit the range of the query. If it is specified, then children that have exactly the specified value must also have exactly the specified key as their key name. This can be used to filter result sets with many matches for the same value.

const getData = async () => {
  const readNewLogEntries = await get(
    query(ref(db, "request-log"), orderByChild("type"), equalTo("Request"))
    // Filters where "type" is equal to "Request". Single arg here ⬆ 
  );
  console.log(readNewLogEntries.val())
  return readNewLogEntries.val();
};

You can read more about the parameters in the documentation

Upvotes: 3

Related Questions