鉴于这两个模型: 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);
});
}
}
});
答案 0 :(得分:3)
我不是向导,但您似乎正在尝试将用户记录设置为邮件:
newMessage.set('user', user);
相反,您应该添加消息:
user.get('messages').addObject(newMessage);