简体   繁体   English

使用hasMany在Ember中创建记录时出错

[英]Error on creating record in Ember with hasMany

I try to create a record which has a oneToMany relationship like this : 我尝试创建一条具有oneToMany关系的记录,如下所示:

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

And

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

In the function model of the ApplicationRoute, I do this : 在ApplicationRoute的功能模型中,我这样做:

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

Which gives the error : 这给出了错误:

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

in the Ember data code at line 5938, in the method reloadHasManys : 在第5938行的Ember数据代码中,在方法reloadHasManys中:

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

And indeed this._data is undefined but should not be as the data ha not yet been initialized at this point in the createRecord method. 确实this._data是未定义的,但不应如此,因为此时尚未在createRecord方法中初始化数据。 I checked my mappings, tried different things but nothing worked. 我检查了映射,尝试了其他操作,但没有任何效果。 I didn't find anything on this problem. 在这个问题上我什么都没找到。 Did I do something wrong. 我做错什么了吗。

I am on the 1.5.1 of Ember and 1.0.0-beta.7.f87cba88 of Ember data 我正在使用Ember的1.5.1和Ember数据的1.0.0-beta.7.f87cba88

You should define your model in the App namespace and reference any relationships by their name, not the actual variable, like this: 您应该在App命名空间中定义模型,并通过它们的名称而不是实际变量来引用任何关系,如下所示:

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

And then when you create a record, you pass in the string name of the type of record you're creating, like this: 然后,当您创建记录时,您传入要创建的记录类型的字符串名称,如下所示:

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

That should fix your problem. 那应该解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM