Ember query result undefined

Hej, i have the following problem:

i wanted to transform this WORKING code:

  myStore.query('test', {}).then(backendData => {

        backendData.forEach(element => {
          var example = element.get('example');
          console.log(example);                  //works. i get a output
          }
        });

into a code without .then and without .foreach, because i want to use yield inside the foreach and i cant because the scope.

so i did this now:

var result = yield myStore.query('test', {});

for(var i = 0; i < result.length; i++)
{
        console.log(result.length);        //length 1
        console.log(result[i]);            //result[i] undefined result isnt null
}

the log says now, that the length of result is 1, but result[i] is undefined. whats the problem? I thought i can access that DS.PromiseArray that i am getting from query via []. or how does it work ?

query returns a Promise that resolves to an AdapterPopulatedRecordArray, which inherits from ArrayProxy (although the docs don’t really show that very well, it seems the links between ember-data’s API docs and Ember’s API docs aren’t working right).

ArrayProxy doesn’t support [] for getting elements, it has objectAt.

I would do what you’re trying to do as:

var result = yield myStore.query('test', {});
for(let item of result.toArray()) {
  console.log(item);
}

(All of this assumes you’re using yield inside an ember-concurrency task. For any future readers confused about yield, see http://ember-concurrency.com )

2 Likes