Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ember async computed属性返回未定义的_Javascript_Ember.js_Ember Data_Indexeddb - Fatal编程技术网

Javascript Ember async computed属性返回未定义的

Javascript Ember async computed属性返回未定义的,javascript,ember.js,ember-data,indexeddb,Javascript,Ember.js,Ember Data,Indexeddb,我试图将属性设置为模型的async、hasMany关系的值。但是我无法在then函数中返回任何值 App.Athlete = DS.Model.extend({ name: DS.attr('string'), age: DS.attr('number'), splits: DS.hasMany('split', {async: true}), times: DS.hasMany('time', {async: true}), }); App.Time = DS

我试图将属性设置为模型的async、hasMany关系的值。但是我无法在
then
函数中返回任何值

App.Athlete = DS.Model.extend({
    name: DS.attr('string'),
    age: DS.attr('number'),
    splits: DS.hasMany('split', {async: true}),
    times: DS.hasMany('time', {async: true}),
});

App.Time = DS.Model.extend({
    athlete: DS.belongsTo('athlete', {async:true}),
    race: DS.belongsTo('race', {async: true}),
    time: DS.attr('string'),
});

App.Split = DS.Model.extend({
    distance: DS.attr('string'),
    time: DS.attr('string'),
    athlete: DS.belongsTo('athlete', {async:true}),
    race: DS.belongsTo('race', {async: true}),
});
我的问题在于
运动员的ObjectController
,我试图将
时间
hasMany数组设置为属性。在
回调函数之外,我可以正常返回值。但是,在
then
回调中,所有值都返回
未定义的值。此外,使用
console.log
我始终能够看到代码正在执行

以下是该方法:

time: function() {
    var raceid= '1';
    this.get('times').then(function(times) {  // returns undefined in this scope
        console.log(times.get('length'));     // consistently displays
        times.forEach(function(time) {
            time.get('race').then(function(timerace) {
                if(timerace.get('id')===raceid) {
                    console.log('match');     // consistently diplays
                    console.log(time.get('time')); // consistently displays
                    return time.get('time'); // returns undefined/not at all
                }
            });
        });
    });
}.property('time.@each.time'),
虽然上面的函数总是返回未定义的
,但下面的函数(同一控制器的一部分)工作一致,没有错误

sortedSplits: function(){
    var self = this,
    raceid = '1',
    splits = this.get('splits');
    // filter each split by race
    this.get('splits').filter(function(split) {
        // retrieve race split belongsTo
        split.get('race').then(function(race) {
            // compare race
            return race.get('id') === thisrace; 
        });
    });
    splits = splits.sortBy('m');
    return splits;                // returns correctly
}.property('splits.@each.m'),
在看了6个小时之后,我不再知道在哪里可以找到这个问题。我不明白这个问题从何而来。其他人会知道这个问题是从哪里产生的吗


注意:我的问题与的问题类似,尽管我在该解决方案中没有任何运气。

您没有向computed属性返回任何内容,而是向回调函数返回一些内容,这些内容将传递给任何链接承诺,但如果您返回承诺,这仍然没有帮助,因为向计算属性返回承诺不会自动解析该承诺并使用计算属性值的结果

您当前的计算属性基本上是这样做的:

time: function() {
    var raceid= '1';
    this.get('times').then(function(times) {  // returns undefined in this scope
        console.log(times.get('length'));     // consistently displays
        times.forEach(function(time) {
            time.get('race').then(function(timerace) {
                if(timerace.get('id')===raceid) {
                    console.log('match');     // consistently diplays
                    console.log(time.get('time')); // consistently displays
                    return time.get('time'); // returns undefined/not at all
                }
            });
        });
    });
  // I'm not returning anything so
  return undefined;
}.property('time.@each.time'),
在这种情况下,最容易使用观察者并设置属性(尽管看起来您是无意中观察时间,而不是时间)

time: null,
timWatcher: function() {
    var raceid= '1',
        self = this;
    this.get('times').then(function(times) {  // returns undefined in this scope
        console.log(times.get('length'));     // consistently displays
        times.forEach(function(time) {
            time.get('race').then(function(timerace) {
                if(timerace.get('id')===raceid) {
                    console.log('match');     // consistently diplays
                    console.log(time.get('time')); // consistently displays
                    self.set('time', time.get('time')); // set property to time value
                }
            });
        });
    });
}.observes('times.@each.time'), //times, not time