簡體   English   中英

使用hasMany在Ember中創建記錄時出錯

[英]Error on creating record in Ember with hasMany

我嘗試創建一條具有oneToMany關系的記錄,如下所示:

App.Models.Esnode = DS.Model.extend({ nodeUrl: DS.attr('string'), nodeState: DS.attr('string'), indices: DS.hasMany(App.Models.Index) });

App.Models.Index = DS.Model.extend({
    name: DS.attr('string')
});

在ApplicationRoute的功能模型中,我這樣做:

this.store.createRecord(App.Models.Esnode, {nodeUrl: "http://192.168.1.13:9200"});

這給出了錯誤:

Error while loading route: TypeError: Cannot read property 'links' of null

在第5938行的Ember數據代碼中,在方法reloadHasManys中:

if (this._data.links && this._data.links[name]) { return; }

確實this._data是未定義的,但不應如此,因為此時尚未在createRecord方法中初始化數據。 我檢查了映射,嘗試了其他操作,但沒有任何效果。 在這個問題上我什么都沒找到。 我做錯什么了嗎。

我正在使用Ember的1.5.1和Ember數據的1.0.0-beta.7.f87cba88

您應該在App命名空間中定義模型,並通過它們的名稱而不是實際變量來引用任何關系,如下所示:

App.Esnode = DS.Model.extend({ 
    nodeUrl: DS.attr('string'), 
    nodeState: DS.attr('string'), 
    indices: DS.hasMany('index') 
});

然后,當您創建記錄時,您傳入要創建的記錄類型的字符串名稱,如下所示:

this.store.createRecord('esnode',{
     nodeUrl: "http://192.168.1.13:9200"
});

那應該解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM