2014-03-02 48 views
3

我試圖用餘燼數據定義我的模型,但由於某種原因,我添加了一些 hasManybelongsTo關係後,出現'Uncaught TypeError:不能調用未定義的方法'modelFor'Ember數據:未捕獲TypeError:無法調用未定義的方法'modelFor'

我在做什麼錯了?

App.User = DS.Model.extend({ 
    username: DS.attr('string'), 
    facebook_id: DS.attr('string'), 
    staff: DS.attr('boolean', {defaultValue: false}), 
    createdAt: DS.attr('date'), 
    posts: DS.hasMany('post', {async: true}), 
    comments: DS.hasMany('comment', {async: true) 
}) 

App.Post = DS.Model.extend({ 
    title: DS.attr('string'), 
    image: DS.attr('string'), 
    track: DS.attr('string'), 
    createdAt: DS.attr('date'), 
    user: DS.belongsTo('user'), 
    comments: DS.hasMany('comment', {async: true}) 
}) 

App.Comment = DS.Model.extend({ 
    user: DS.belongsTo('user'), 
    post: DS.belongsTo('post'), 
    track: DS.attr('string'), 
    createdAt: DS.attr('date') 
}) 

回答

1

通過在關係中指定應用名稱來解決它,例如,而不是hasMany('comment')我使用hasMany('App.Comment')。不知道發生了什麼,因爲前者是文檔中顯示的內容。

App.User = DS.Model.extend({ 
    username: DS.attr('string'), 
    facebook_id: DS.attr('string'), 
    staff: DS.attr('boolean', {defaultValue: false}), 
    createdAt: DS.attr('date'), 
    posts: DS.hasMany('App.Post', {async: true}), 
    comments: DS.hasMany('App.Comment', {async: true) 
}) 

App.Post = DS.Model.extend({ 
    title: DS.attr('string'), 
    image: DS.attr('string'), 
    track: DS.attr('string'), 
    createdAt: DS.attr('date'), 
    user: DS.belongsTo('App.User'), 
    comments: DS.hasMany('App.Comment', {async: true}) 
}) 

App.Comment = DS.Model.extend({ 
    user: DS.belongsTo('App.User'), 
    post: DS.belongsTo('App.Post'), 
    track: DS.attr('string'), 
    createdAt: DS.attr('date') 
}) 
0

還要注意的是,當你引用一個模型不存在,例如,你可以得到這樣的錯誤:

App.ItServiceUser = DS.Model.extend({ 
    companyService: DS.belongsTo('App.CompanyITService'), 
    employee: DS.belongsTo('App.AllEmployee'), 
    employeeID: attr('string'), 
    isLeadAdmin: attr('string'), 
    isAdmin: attr('boolean'), 
    isEditing: attr('boolean'), 
    username: attr('string'), 
    firstName: attr('string'), 
    lastName: attr('string'), 
    userChoiceSet: attr('string') 
}) 

冉從控制檯驗證碼:

> user = App.ItServiceUser.find(2216) 
g {id: "2216", store: g, _reference: Object, stateManager: (...), _changesToSync: Object…} 
> user.set("lastName", "Anthony") 
g {id: "2216", store: g, _reference: Object, stateManager: (...), _changesToSync: Object…} 
> user.save() 
q {_promiseCallbacks: Object, constructor: function, then: function, on: function, off: function…} 
Uncaught TypeError: Cannot read property 'toString' of undefined ember-data.js:2540 

刪除行參考一個不存在的模型,我很好:

companyService: DS.belongsTo('App.CompanyITService'), 
相關問題