6

I'm new to ember.js and firebase. I've been trying to make something that needs me to query the DB for a key matching a defined value.

According to guides.emberjs.com, the following example should work:

this.store.query('person', { filter: { name: 'Peter' } }).then(function(peters) {
  // Do something with `peters`
});

But it doesn't. Apparently because I'm using the emberfire addon. After literally hours of googling, there was no clear solution.

The emberfire docs talk about the available arguments.

Arguments

orderBy - String - The property ...
.
.
.

equalTo - String, Number, Null - Creates a query which includes children which match the specified value.

And present an example...

// app/routes/dinosaurs.js
export default Ember.Route.extend({
  model: function() {
    return this.store.find('dinosaur', {
      orderBy: 'height',
      limitToLast: 10,
      startAt: 5
    });
  }
});

Although without showing how to use 'equalTo'. I've tested all of them, but I failed to understand how equalTo works.

There are other solutions on SO about this, but they are all pre-v2.0.0. So I don't think they would work post v2.0.0.

Ember.js debug info:

DEBUG: -------------------------------
DEBUG: Ember      : 2.0.0
DEBUG: Ember Data : 2.0.0
DEBUG: Firebase   : 2.3.1
DEBUG: EmberFire  : 1.6.0
DEBUG: jQuery     : 1.11.3
DEBUG: -------------------------------

The database in use: https://shoutoutdb.firebaseio.com/users

I don't fully understand how equalTo should work here, but I'm not getting any clue either. Hopefully someone here will be willing to help.

If you think the question needs any kind of improvement, kindly ask. I've detailed it as well as I thought I should.
Thanks in advance. :)

EDIT: Code I tried to use:

$E.store.find('user',{name:'Alpha'}).then(
    function (data) {
       //stuff done with the data
    }
);

I also tried multiple different versions around this code. Nothing worked, so I don't think it's even worth mentioning them here.

Jayant Bhawal
  • 1,300
  • 2
  • 21
  • 27
  • Since people are down voting this, can they at least tell me why? – Jayant Bhawal Oct 12 '15 at 08:33
  • What error are you seeing? – vikram7 Oct 12 '15 at 15:04
  • 1
    @vikram7 I'm not getting an error. I'm getting everything! In the code I pasted at the bottom of the question just now, I don't know if it should work, but it's certainly not giving me what I want. I'm not asking a "I'm getting some error, can't resolve." question, I'm asking a "I want to do 'this', and I tried everything I could think of, nothing works." question. – Jayant Bhawal Oct 12 '15 at 17:47
  • 1
    @JayantBhawal I don't know why people are downvoting this. It's exactly the problem I'm having too. One thing I did notice from the docs is that `query` passes through to the back-end. So, it is going to be specific to Firebase in this case _not_ Ember. – user341493 Nov 30 '15 at 03:12
  • Even in that case, it'll be great if someone had an idea what to do. Since I never really said this is an Ember specific issue. I honestly looked up a lot on this. – Jayant Bhawal Dec 04 '15 at 16:31

2 Answers2

15

Using the querying capabilities you can combine orderByChild and equalTo to get the desired result:

ref.child('people').orderByChild('name').equalTo('Peter').on('value', ...);

There is an example in the web API reference for equalTo and in the Web guide on complex queries.

So, in EmberFire, this would translate to the following:

this.store.query('person', { orderBy: 'name', equalTo: 'Peter' });
tstirrat
  • 1,056
  • 9
  • 9
  • Hey @tstirrat that works!! thank you.. I need to filter by more than one property like `name: 'Peter', age: 20`, how to do this? thanks. – Joe Hany Nov 14 '16 at 19:06
  • Hey Joe. Look at answer for how to go about this. It's included in the add-on. – Tom Aug 01 '17 at 16:06
1

Although tstirrat's response will get you by in most cases, There is an Ember addon for querying a firebase search.

You can check it out here:

https://www.npmjs.com/package/ember-emberfire-find-query

Tom
  • 300
  • 3
  • 17