Search code examples
ember.jsember-dataember-cli

Ember Iterating through a promise/ store.findAll return value


In my router I have code let users= this.store.findAll('user')

And my model user.js will be name: DS.attr('string'), userName: DS.attr('string'), company: DS.attr('string')

In my mirage fixture I have User object defined as [{'name':'smith','userName':'smith123'},{'name':'james','userName':'james222'}

And in my router when I do let users= this.store.findAll('user') I want to iterate through users and add company manually for each user. But I am unable to find the way to access the user objects in router js file.

And same object I can iterate in .hbs file. But unable to find the way to iterate it in router js file. Can you please let me know the way to do this.


Solution

  • findAll method (and findRecord too) returns a promise, not iterable object. You can iterate through users after promise will be resolved. To do this, you should use then method:

    this.store.findAll('user')
      .then(users => {
        /*Iterate here*/
      })
      .catch(error => {
        /*Do something to inform user about network/server/request error here*/
      });