javascript - 服务中的函数未返回正确的值

标签 javascript ember.js

我有一个仅以数组形式提供数据的服务:

// services/countries.js
import Ember from 'ember';

export default Ember.Service.extend({

   countries: [
   {
      "name": "Afghanistan",
      "code": "AF"
   },
   ....
   ]
});

我可以在助手中成功访问:

// helpers/countries.js
export default Ember.Helper.extend({
    countries: Ember.inject.service('countries'),
    compute(params, hash) {
        console.log(this.get('countries.countries'));
        return 'test';
    }
});

现在我向该服务添加了一个函数来搜索给定的国家/地区代码并返回匹配的国家/地区:

// in services/countries.js
...
getByCode: function(code) {
    this.get('countries').forEach(function(item) {
        if(item.code===code) {   // finds the right item
            console.log('returning item:');
            console.log(item);   // outputs the right object
            return item;         // I expected to have the same item retured..
        }
    });
return {name:'not found', code: ''};
},
...

当我在助手中调用该函数时

// in helpers/countries.js
...
compute(params, hash) {
   let country = this.get('countries').getByCode('DE');
   console.log(country); // outputs {name: 'not found',..} instead of the found and returned(?) item
   return country.name;
}
...

请注意,正确的输出(服务中的 console.log)位于“错误”输出之前:

// console output
returning item:   roles.js:6 
Object {name: "Germany", code: "DE", nameLocal: "Deutschland"}   hash.js:2
Object {name: "not found", code: ""}

同样让我好奇的是,在控制台中提到了“错误的”.js(roles.js - 这是另一个服务,没有此功能)

所以我的问题是为什么我会得到不同的项目返回/输出?

为了完整性: 我在模板中仅使用此助手一次,如下所示:

{{#if model.country}}{{countries model.country}}{{/if}}

(当然也会输出“错误”的国家/地区)

Ember-CLI 1.13.7 Ember 2.0.1

最佳答案

forEach 循环中的 return 存在问题。

There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool.

如果您想使用forEach,请将您的函数修改为:

getByCode: function(code) {
    var found = null;
    this.get('countries').forEach(function(item) {
        if(item.code === code) {
            found = item;
        }
    });
    return found != null ? found : {name:'not found', code: ''};
},

更多信息请点击:EmberJS: is it possible to break from forEach?

但是,我建议改用它:

getByCode: function(code) {
   let country = this.get('countries').find(c => c.code === code);
   return country != undefined ? country : {name:'not found', code: ''};
}

关于javascript - 服务中的函数未返回正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35821529/

相关文章:

javascript - z-index 不是显性的吗?

javascript - 当您有 'Include' 的新数据时,更新/刷新 Page-Mod 的最佳方法是什么?

javascript - Meteor、Ember.js 和 Backbone.js 之间的主要区别是什么?

ember.js - Ember-data - "Attempted to handle event ` deleteRecord` on *record* 而处于 root.deleted.saved 状态。”

javascript - 如何在我的 ember View 中绑定(bind)到 bootstrap 的模态事件?

javascript - String 和 Array 泛型方法将在未来被弃用

javascript - 如何获取 ENV 类型 Laravel + VueJS + Homestead

javascript - ScrollView 子布局必须通过 contentContainerStyle Prop 应用

javascript - 使用模型中的 Ajax 数据填充 Ember JS Handlebars 模板?

javascript - 将 Ember 与 Rails 4 集成后出现断言失败错误