【问题标题】:EmberJS 2.7 assertion failed passing id of nested object [closed]EmberJS 2.7断言传递嵌套对象的ID失败[关闭]
【发布时间】:2016-12-30 05:04:35
【问题描述】:

EmberJS 2.7 错误: 断言失败:id 传递给findRecord() 必须是非空字符串或数字

app/templates/products/index.hbs: [在模型/每个循环中我都有这一行]:

{{#link-to 'categories.edit' product.category.id}}<a href="">{{product.category.name}}</a>{{/link-to}}

app/router.js: [我定义了这些路线]:

  this.route('products', function() {
    this.route('new');
    this.route('edit', { path: '/:product_id/edit' });
  });

  this.route('categories', function() {
    this.route('new');
    this.route('edit', { path: '/:category_id/edit' });
  });

它在编辑产品时起作用。但是在尝试编辑类别时会抛出上述错误。

如果我删除“类别/编辑”路线并添加此路线:

this.route('category', { path: '/categories/:category_id/edit' });

并更改模板以使用它:

{{#link-to 'category' product.category.id}}<a href="">{{product.category.name}}</a>{{/link-to}}

然后就可以了。我明白为什么第二个有效。 但为什么第一个不起作用?

编辑:这里是模型

app/models/product.js:

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
  category: DS.belongsTo('category', { async: true })
});

app/models/category.js:

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  products: DS.hasMany('product')
});

【问题讨论】:

  • “这个问题的解决方式不太可能帮助未来的读者”——我完全不同意这一点。如果您搜索 SO,您会看到我引用的特定错误有很多原因,对于刚接触 EmberJS 的人来说,试图找出导致该错误的原因可能非常困难。我花了相当多的时间自己阅读它们并试图找出在我的代码中引发错误的原因。当您将完整的代码块放入错误的文件并触发此错误时,这不是“简单的错字”。

标签: ember.js ember-router htmlbars


【解决方案1】:

已修复。将 findRecord 放在模型上的位置很重要。我在类别路线中有此代码。将其移至正确的文件即可解决问题。

app/routes/categories.js:

import Ember from 'ember';

export default Ember.Route.extend({
    model(params) {
        return this.store.findRecord('category', params.category_id);
    },
});

解决方案

app/routes/categories.js:

import Ember from 'ember';

export default Ember.Route.extend({
});

app/routes/categories/edit.js:

import Ember from 'ember';

export default Ember.Route.extend({
    model(params) {
        return this.store.findRecord('category', params.category_id);
    },
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-22
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    相关资源
    最近更新 更多