【问题标题】:Error while trying to self reference a model in ember.js尝试在 ember.js 中自我引用模型时出错
【发布时间】:2017-08-26 05:44:21
【问题描述】:

我正在尝试在模型中创建自引用属性,但出现以下错误:

处理路由时出错:index No model were found for 'menu' Error

这是我的模型(menus.js):

import DS from 'ember-data';

export default DS.Model.extend({    
  title: DS.attr('string'),
  link: DS.attr('string'),
  isActive: DS.attr('boolean'),
  children: DS.hasMany('menus', { inverse: null }),
});

我正在尝试从 Web API 返回一个列表。这是我的 API JSON 返回:

[
  {
    "id": 1,
    "title": "Test",
    "link": "index",
    "isActive": false,
    "children": [
      {
        "id": 4,
        "title": "Test 2",
        "link": "index",
        "isActive": false
      }
    ]
  },
  {
    "id": 2,
    "title": "Test 2",
    "link": "index",
    "isActive": false,
    "children": [
      {
        "id": 5,
        "title": "Test 4 ",
        "link": "index",
        "isActive": false
      }
    ]
  },
  {
    "id": 3,
    "title": "Test 5",
    "link": "index",
    "isActive": false,
    "children": [
      {
        "id": 6,
        "title": "Test 6",
        "link": "index",
        "isActive": false
      },
      {
        "id": 7,
        "title": "Test 7",
        "link": "index",
        "isActive": false
      }
    ]
  }
]

【问题讨论】:

    标签: javascript c# json asp.net-web-api ember.js


    【解决方案1】:

    您应该以单数形式调用您的模型。因此,将 model/menus.js 重命名为 model/menu.js 并将 DS.hasMany('menus', { inverse: null }), 重命名为 DS.hasMany('menu', { inverse: null }),

    【讨论】:

    • 感谢您的帮助,力士!但是现在当我调用 findAll 方法时出现此错误:“断言失败:您不能再将 modelClass 作为第一个参数传递给 store.buildInternalModel。改为传递 modelName。” return this.get('store').findAll('menu');
    • 那个错误不是来自那个代码。可能您将模型而不是字符串传递给findAll
    猜你喜欢
    • 1970-01-01
    • 2018-11-06
    • 2023-04-07
    • 2011-11-13
    • 1970-01-01
    • 2019-06-03
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多