How to use emberfire to query a firebase db for all items with name equalTo xyz in ember.js 2.0.0?

邮差的信 提交于 2019-12-06 02:33:53

问题


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.


回答1:


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' });



回答2:


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



来源:https://stackoverflow.com/questions/33071340/how-to-use-emberfire-to-query-a-firebase-db-for-all-items-with-name-equalto-xyz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!