出于某种原因,我在尝试使用ember store创建记录时不断收到此错误。
以下是错误:Uncaught Error: No model was found for 'user_id'
这是导致错误的我的动作函数。我已经从模型和创建中移除了user_id
以及任何使用user
模型的内容,它仍然给出了相同的错误。我根本无法弄清楚导致错误的原因,错误本身也不是很有用。它没有给我任何导致我的代码中的错误的行号。
addProposal: function() {
var store = this.store;
var self = this;
var foundUser = function(user) {
console.log(user);
var proposal = store.createRecord('proposal', {
discount_code: self.get('discount_code'),
user_id: user,
status_id: self.get('status_id'),
timeline: self.get('timeline'),
expiry_dt: self.get('expiry_dt'),
company_name: self.get('company_name'),
client_email: self.get('client_email'),
proposal_needed_by: self.get('proposal_needed_by'),
data_needed_by: self.get('data_needed_by'),
proposal_expiry_dt: self.get('proposal_send_dt'),
liason_id: user,
qb_id: self.get('qb'),
version: self.get('version'),
custom_discount: self.get('custom_discount'),
notes: self.get('notes'),
});
var onSuccess = function(proposal) {
console.log('Proposal was created');
this.transitionToRoute('proposal', proposal);
};
var onFailure = function() {
console.log('Something went wrong');
};
proposal.save().then(onSuccess, onFailure);
};
store.find('user', $.cookie('user_id')).then(foundUser);
//console.log(this.get('statuses'));
}
以下是我的提案模式:
import DS from 'ember-data';
import Ember from 'ember';
var attr = DS.attr,
hasMany = DS.hasMany,
belongsTo = DS.belongsTo;
export default DS.Model.extend({
discount_code: attr(),
user_id: belongsTo('user', {async: true}),
user: Ember.computed.alias('user_id'),
status_id: belongsTo('status', {async: true}),
status: Ember.computed.alias('status_id'),
timeline: attr(),
proposal_needed_by: attr(),
data_needed_by: attr(),
proposal_expiry_dt: attr(),
company_name: attr(),
client_name: attr(),
client_email: attr(),
sent_dt: attr(),
liason_id: belongsTo('user', {async: true}),
// Semantically better
liason: Ember.computed.alias('liason_id'),
qb_id: belongsTo('user', {async: true}),
qb: Ember.computed.alias('qb_id'),
version: attr(),
custom_discount: attr(),
notes: attr(),
created_at: attr(),
friendly_date: function() {
return moment(this.get('created_at')).format('MMM Do YY, h:mm a');
}.property('created_at'),
updated_at: attr(),
comments: hasMany('comment', {async: true}),
logs: hasMany('log', {async: true}),
});
答案 0 :(得分:2)
服务器很可能返回带有密钥user_ids
的哈希,导致ember查找名为user_id
的模型,而在您的情况下,服务器应返回密钥users
。
如果您无法更改服务器,则可以编写自定义适配器,该适配器将更改来自服务器的有效负载中的密钥名称。