![](/img/trans.png)
[英]Ember.js belongsTo relationship create/edit in select element (dropdown)
[英]Creating record with belongsTo relationship error - Ember.js
给定两个模型:Message.js和User.js,尝试使用Ember.js创建记录时,我不断收到以下错误。 任何Ember.js向导都知道我的代码有什么问题吗?
//错误:断言失败:您只能向此关系添加“用户”记录
App.Message = DS.Model.extend({
title: DS.attr(),
body: DS.attr(),
user: DS.belongsTo('user'),
});
App.User = DS.Model.extend({
first_name: DS.attr(),
last_name: DS.attr(),
email: DS.attr(),
password: DS.attr(),
messages: DS.hasMany('message'),
});
App.NewMessageController = Ember.Controller.extend({
need: ['application'],
thisUserID: Ember.computed.alias('controllers.application.userID'),
actions: {
save: function() {
// Prints -> 'thisUserID' -- all is fine here
console.log('The user id from new-letter is: '+this.get('thisUserID'));
var store = this.store;
//Prints -> <DS.PromiseObject:ember433> -- Strange?
console.log("Checking the store: "+store.find('user', this.get('thisUserID')));
var newMessage = store.createRecord('message', {
title: this.get('title'),
body: this.get('body'),
});
newMessage.save();
// Prints-> Error: Assertion Failed: You can only add a 'user' record to this relationship
var user = store.find('user', this.get('thisUserID')).then(function(user) {
newMessage.set('user', user);
});
}
}
});
我不是向导,但是您似乎正在尝试将用户记录设置为消息: newMessage.set('user', user);
相反,您应该添加以下消息: user.get('messages').addObject(newMessage);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.