EmberJS“Assert失败:在卸载并重新加载模型后调用已破坏对象上的集合”

omtl5h9j  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(100)

我在卸载数据模型并在进行API调用时正确地重新填充它们时遇到了麻烦。
型号:

/* Model Foo */
export default DS.Model.extend({
  bars: DS.hasMany('bar', { async: true })
});

/* Model Bar */
export default DS.Model.extend({
  foo: DS.belongsTo('foo', { async: true, inverse: 'bars' })
});

在应用中的某个点,foo和bar都从ember数据存储中卸载,然后从API调用中重新加载。

/* Unload and reload snippet */
this.store.unloadAll('bar');
this.store.unloadAll('foo');

let bars = this.store.filter('bar', { 
  queryParam: x 
}, function(bar) {
  return x === bar.x
});

let foos = this.store.filter('foo', { 
  queryParam: y 
}, function(foo) {
  return y === bar.y
});

let self = this;
Ember.RSVP.all([foos, bars]).finally(function() {
  self.controller.set('model.foos', foos);
  self.controller.set('model.bars', bars);
});

问题出现在依赖于这些模型变化的计算属性中。

/* Computed property elsewhere in app */
compProp: Ember.computed('foo.bars.[]', function() {
  let tmp = this.get('foo.bars'); /* <-- Error generating line */
  .
  .
  .
})

这一行给出了以下错误:Assertion Failed: calling set on destroyed object: <DS.PromiseManyArray:ember1995>.content = <DS.ManyArray:ember3320>
来自错误的堆栈跟踪:

Assertion Failed: calling set on destroyed object: <DS.PromiseManyArray:ember1014>.content = <DS.ManyArray:ember1348>
Error
    at assert (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:23072:13)
    at Object.assert (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:23285:34)
    at Object.set (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:39281:17)
    at Class.set (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:52151:26)
    at ManyRelationship._updateLoadingPromise (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:152522:33)
    at ManyRelationship.getRecords (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:152723:21)
    at Class.get (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:152101:60)
    at ComputedPropertyPrototype.get (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:34367:28)
    at get (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:39184:19)
    at _getPath (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:39205:13)
    at Object.get (http://localhost:4200/assets/vendor-3e8430b320dbe268f7ee6486de4c6cad.js:39180:14)

谢谢你的帮助!
附注:

  • 我知道store.filter()已经过时了。我正在从早期的Ember版本升级应用程序,并使用ember-data-filter插件来临时兼容。
  • 这在Ember 1.13上是有效的。

更新

为了不让这个问题悬而未决,我从来没有能够找到这个问题。
对我来说,解决方案是删除所有unloadAll()呼叫,并根据需要单独管理记录。

2jcobegt

2jcobegt1#

这可能是由任何异步set在一个被ember破坏的元素上发生引起的。因此,它可能是棘手的跟踪。我通常在chrome开发者控制台打开“暂停捕获异常”来找到导致问题的set

相关问题