【问题标题】:emberjs hasMany returns undefinedemberjs hasMany 返回 undefined
【发布时间】:2013-02-13 06:27:18
【问题描述】:

我正在尝试使用固定装置和两个模型之间的一对多关系来实现一个最小的 emberJs 应用程序:

App.store = DS.Store.create({
  revision: 11,
  adapter: 'DS.FixtureAdapter'
});

App.Album = DS.Model.extend({
  Name: DS.attr("string"),
  Songs: DS.hasMany('App.Song')
});

App.Song = DS.Model.extend({
  Name: DS.attr("string"),
  Album: DS.belongsTo('App.Album')
});

App.Album.FIXTURES = [
  {
    id: 1,
    Name: 'foo'
  },
  {
    id: 2,
    Name: 'bar'
  }
];

App.Song.FIXTURES = [
  {
    id: 1,
    Album_id: 1,
    Name: "asdf"
  },
  {
    id: 2,
    Album_id: 2,
    Name: "Test"
  }
];

我可以像这样通过控制台访问专辑模型 App.Album.find(1).get('Name') # => foo

每当我尝试通过专辑和歌曲之间的关系访问 Songs 属性时,我都会得到未定义:

App.Album.find(1).get('Songs').objectAt(0) # undefined

任何提示我在这里可能做错了什么?

【问题讨论】:

    标签: javascript ember.js


    【解决方案1】:

    您尚未定义 Songs 和 Album 具有哪个。您需要在 Album 模型中指定 Songs: [1,2,3]

    (很确定是Songs,但也可能是Song_ids。)

    【讨论】:

    • 不用担心。是Songs吗?
    • 属性名是不是不能小写?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多