0
votes

For some reason I keep getting this error when trying to create a record using ember store.

Here is the error: Uncaught Error: No model was found for 'user_id'

Here is my actions function that causes the error. I've removed user_id and anything that uses the user model from the model as well as from the creation and it still gives me the same error. I simply can't figure out what's causing the error, the error itself isn't very informative either. It's not giving me any line numbers that are causing the error in my code.

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'));

}

Here is my proposal model:

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}),
});
1
Just to let you know, there's an issue with setting async belongs to, github.com/emberjs/data/issues/1542#issuecomment-33594385 and github.com/emberjs/data/pull/1535Kingpin2k

1 Answers

2
votes

Most likely the server is returning a hash with key user_ids, causing ember to look for a model called user_id, whereas in your case the server should be returning the key users.

If you can't change the server you can write a custom adapter which will change the key name in the payload coming from the server.