How can I avoid resolving promises after a common find on the ember data model?

I need to check the ember-data model returning from the base find manually (this model is not tied to a template and instead I need to dynamically apply some logic)

So far I have tried the following (with no luck)

App.Foo.find().then(function(model) {
  console.log("here with the ember-data payload");
  console.log(model.get('length'));
}, function(error) {
  console.log("broken");
});

      

The success block fires, but it always returns 0 results, but when I view the network tab in chrome it shows a valid json payload that works outside of this promise that I am using.

is it possible to grab the resolution of a promise using ember data rev 11?

+3


source to share


2 answers


App.Foo.find()

returns a list of models, namely DS.AdapterPopulatedRecordArray

, which is not an array and therefore does not have a length property. But it has a content property, which is an array of models. So in your example, you have to use console.log(model.content.length);

to make it work:

App.Foo.find().then(function(result) {
  console.log("here with the ember-data payload");
  console.log(result.content.length);
  console.log(result.objectAt(0));
}, function(error) {
  console.log("broken");
});

      



Note that you cannot use the []

on operator DS.AdapterPopulatedRecordArray

because it is not an array. See the Ember Guide to DS . Instead, use objectAt

: result.objectAt(0);

.

+5


source


App.Model.find().then(function(notes) {console.log(notes.content.length)})

returned 5 for me, which is the correct return value for my application.



I think your syntax is correct, although perhaps you are having problems with the data your server is returning? It might not serialize to actual records for whatever reason. It might be worth double checking.

+2


source







All Articles