1
votes

I'm new to Ember, and have been impressed so far by how succinct everything is. Though querying data with EmberFire seems a little odd. For instance, in a component's js I can find a record with :

store.query('user', { orderBy: 'email', equalTo: email }).then((response) => {
  // expect response to be an object, or an array of objects
  // but its actually a class, and needs the following to return data      
  var user = response.get('content')[0]._data;

  // now able to access properties as originally expected
  console.log(user.email);
});

While the above works, it doesn't feel very elegant. Am I missing something? As far as I can see the documentation stops at explaining the query, so any help as to the proper way of accessing a model's properties would be much appreciated.

Thanks

2

2 Answers

0
votes

store.query returns a promise that resolves to a AdapterPopulatedRecordArray. So yes, it should be returning a class.

If you're expecting only 1 record to be returned, then you should use queryRecord.

store.queryRecord('user', { orderBy: 'email', equalTo: email }).then((user) => {
  // now able to access properties as expected
  console.log(user.get('email'));
});

If you're expecting more than 1 record, keep using query and you'll be able to loop through the user results that are returned.

0
votes

This problem has existed for a while and it's because EmberFire was deprecated before anybody wrote an elegant way to query this way.

This add-on solves the problem, and will let you query pretty much any way you can imagine with the filterCustom method.