Ember通过promise / store.findAll返回值迭代

时间:2017-08-14 19:12:39

标签: ember.js ember-data ember-cli

在我的路由器中,我有代码let users= this.store.findAll('user')

我的模型user.js将是name: DS.attr('string'), userName: DS.attr('string'), company: DS.attr('string')

在我的海市蜃楼夹具中,我将User对象定义为[{'name':'smith','userName':'smith123'},{'name':'james','userName':'james222'}

在我的路由器中let users= this.store.findAll('user')我希望遍历users并为每个用户手动添加company。但我无法找到访问路由器js文件中的用户对象的方法。

我可以在.hbs文件中迭代相同的对象。但无法找到在路由器js文件中迭代它的方法。能否告诉我这样做的方法。

1 个答案:

答案 0 :(得分:0)

findAll方法(以及findRecord)也会返回promise,而不是可迭代的对象。您可以在解决承诺后迭代用户。为此,您应该使用then方法:

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