3

Angular application using AngularFire2. I'm attempting to query firebase on a child object, rather than just a child string property. Querying by the child string property works, but how can I query by the entire object.

This code works

    // Given the following firebase data structure
    httpCache: {
        {ID}: {
            url: 'www.fakeurl',
            otherProperties: {}
        }
    }

    // This code will return results if exists where the url matches
    this.af.database.list('httpCache', {
        query: {
           orderByChild: 'url',
           equalTo: 'www.fakeurl'
        }
    }).subscribe(x => {
        if (x.length > 0) { console.log('match found!'); }
    });

This code does not work:

    // Given the following firebase data structure
    httpCache: {
        {ID}: {
            request: {
                url: 'www.fakeurl',
                params: 'id=1'
            },
            otherProperties: {}
        }
    }

    // This code throws an exception
    let request = {
        url: 'www.fakeurl',
        params: 'id=1'
    };
    this.af.database.list('httpCache', {
        query: {
            orderByChild: 'request',
            equalTo: request
        }
    }).subscribe(x => {
        if (x.length > 0) { console.log('match found!'); }
    });

Here is the exception:

Query: First argument passed to startAt(), endAt(), or equalTo() cannot be an object.

I'm attempting to use the 2nd solution shown here where the filter criteria is pushed into a child object containing all filter properties: Query based on multiple where clauses in firebase

Community
  • 1
  • 1
Ryan Langton
  • 6,294
  • 15
  • 53
  • 103
  • Quick stab below. If you share a snippet of your JSON (as text, no screenshots), I can match the code to that. But the approach will be as I describe below. – Frank van Puffelen Sep 26 '16 at 14:44

1 Answers1

0

You cannot query on an object. So this is invalid and causes the error:

let request = {
    url: 'www.fakeurl',
    params: 'id=1'
};
this.af.database.list('httpCache', {
    query: {
        orderByChild: 'request',
        equalTo: request
    }
})

As I answer in the question you linked, you can add a property that contains the combined values that you want to query on. But then you'll also need to combine the values in your actual query call, which you're not doing.

It's impossible to say how you combined the values, but say that you simply concatenated the URL and the params, you'd query as:

let request = 'www.fakeurl_id=1'
this.af.database.list('httpCache', {
    query: {
        orderByChild: 'request',
        equalTo: request
    }
})
Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807