Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 889
Jayant Bhawal Avatar asked Oct 11 '15 23:10

Jayant Bhawal


People also ask

How do I read data from Firebase in a query?

The first one is to read the data using a persistent listener, meaning that we’ll always be in sync with the Firebase servers, or we can read the data only once. The first option is very helpful when we need to listen for changes in real-time. We can achieve this using Query ’s addValueEventListener () method.

What is an asynchronous listener in Firebase?

Asynchronous listeners: Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener is triggered once for the initial state of the data and again anytime the data changes. An event listener may receive several different types of events.

What is a product object in Firebase Realtime?

As you can see in the database schema, we have a node of products, meaning that each child within that node is a Product object. Here is the corresponding data class: Because the result of a Firebase Realtime Database call, will always return either the products or an Exception, we’ll use a Response data class that looks like this:

What is the difference between Cloud Firestore and Firebase Realtime Database?

This method remains part of the Firebase Realtime Database Android SDK ever since. On the other hand, Cloud Firestore, the newer massively scalable NoSQL cloud-hosted real-time database from Google, has another type of mechanism for reading the data once.


2 Answers

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' });
like image 173
tstirrat Avatar answered Nov 15 '22 09:11

tstirrat


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

like image 27
Tom Avatar answered Nov 15 '22 11:11

Tom