angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Query a node which is an array itself

Open joyceview opened this issue 10 years ago • 6 comments

Hi there,

Is there a way to query a node which is an array? What I want is return this node of "-KO8cSA1a6Tl21vHJao5" when today is Monday or Tuesday.

"data" : { "-KO8cSA1a6Tl21vHJao5" : { "repeatDays" : [ "Mon", "Tue" ], "updatedOn" : "Mon Aug 01 2016 22:06:12 GMT-0700 (PDT)" } }

I also tried to save the value as a string instead of string[], but can't get the result either.

Could anyone help me with this?

Thanks a lot!

joyceview avatar Aug 02 '16 05:08 joyceview

You mean, you want to get the nodes that has a repeatDays of Mon && Tue ?

lexcaraig avatar Aug 02 '16 06:08 lexcaraig

Yes, If today is Mon or Tue, I want to get the node.

joyceview avatar Aug 02 '16 06:08 joyceview

You can refer to this issue #396

lexcaraig avatar Aug 02 '16 06:08 lexcaraig

I'm using the filter. The filter is working only if the value in firebase is a string. But not working if the value is an array, nor as a string like "Mon,Tue". As my today's variable could only be "Mon" or "Tue", but my RepeatDays in database could be up to 7 days.

If there is a comparison of Contains or something alike, that might work.

joyceview avatar Aug 02 '16 06:08 joyceview

This should return an array of all items that match repeatDays: 'Mon,Tue'

import { ReplaySubject } from 'rxjs/ReplaySubject';

const equalTo$ = new ReplaySubject(1);

const query$ = af.database.list('/data', {query: {
  orderByChild: 'repeatDays',
  equalTo: equalTo$
}});

equalTo$.next('Mon,Tue');

query$.subscribe(data => console.log(data)); //=> [{repeatDays : 'Mon,Tue' ... }]

r-park avatar Aug 02 '16 15:08 r-park

Sorry to bother again, but the issue is still not solved. I did try the code above, but it's not working.

The scenario is this: KeyValue pair saved in database is this: { repeatDays: 'Mon, Tue' } And today's day name is: 'Mon'

The result I'm expecting is, I'll be able to get the data node with the key value pair as above. Today's day name is only partial of the data saved in database.

The answer I got above can only get me the data node when the value are exactly same. I need to get the value when it's partially there.

joyceview avatar Aug 13 '16 02:08 joyceview