简体   繁体   中英

Uncaught TypeError: Object user has no method ‘_create’

I'm receiving this message when attempting to save a new record. I recreated the walkthrough on SmashingMag's website. I am using ember data v0.14. Looking at the commits it seems two others are having the same issue (possibly related to RESTAdapter). Could anyone provide an answer why?

App = Ember.Application.create({});
App.Store = DS.Store.extend();

App.User = DS.Model.extend({
    first_name: DS.attr('string'),
    last_name: DS.attr('string'),
    email: DS.attr('string'),
    password: DS.attr('string'),
    created_at: DS.attr('string'),
    updated_at: DS.attr('string'),

    fullName: function() {
        return this.get('first_name') + ' ' + this.get('last_name');
    }.property('first_name','last_name')
});

App.Router.map(function(){
    this.resource('users',function(){
        this.resource('user', {path: '/:user_id'}, function(){
            this.route('edit');
        });
        this.route('create');
    });
});

App.IndexRoute = Ember.Route.extend({
    redirect: function() {
        this.transitionTo('users');
    }
});

App.UsersRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('user');
    }
});

App.UserRoute = Ember.Route.extend({
    model: function(params) {
        return this.store.find('user', params.user_id);
    }
});

App.UserEditRoute = Ember.Route.extend({
    model: function() {
        return this.modelFor('user'); // get the same model as the UserRoute
    }
});

App.UsersCreateRoute = Ember.Route.extend({
    model: function(){
        // the model for this route is a new empty Ember.Object
        return Em.Object.create({});
    },

    // in this case (the create route) we can re-use the user/edit template
    // associated with the usersCreateController
    renderTemplate: function(){
        this.render('user.edit', {
            controller: 'usersCreate'
        });
    }
});

App.UsersController = Ember.ArrayController.extend({
    sortProperties: ['first_name'],
    sortAscending: true,
    usersCount: function() {
        return this.get('model.length');
    }.property('@each')
});

App.UserController = Ember.ObjectController.extend({
    deleteMode: false,

    actions: {
        edit: function() {
            // we need transitionToRoute because we are in a controller, otherwise it's transitionTo when in a route
            this.transitionToRoute('user.edit');
        },
        delete: function() {
            this.toggleProperty('deleteMode');
        },
        cancelDelete: function() {
            this.set('deleteMode',false);
        },
        confirmDelete: function() {
            this.get('model').deleteRecord();
            this.get('model').save();
            this.transitionToRoute('users');
            this.set('deleteMode',false);
        }
    }
});

App.UserEditController = Ember.ObjectController.extend({
    actions: {
        save: function() {
            var user = this.get('model');
            user.save();
            this.transitionToRoute('user',user);
        }
    }
});

App.UsersCreateController = Ember.ObjectController.extend({
    needs: ['user'],

    actions: {
        save: function () {
            // just before saving, we set the creationDate
            this.get('model').set('creationDate', new Date());
            // create a new user and save it
            var newUser = this.store.createRecord('user', this.get('model'));
            newUser.save();

            // redirects to the user itself
            this.transitionToRoute('user', newUser);
        }
    }
});

看来答案是您需要升级到余烬数据1.0,很高兴如此简单;)

I was having the same problem and tried around for some hours. I knew create on the model wouldn't work but I tried it anyway and the following error message showed up:

Uncaught Error: You should not call create on a model. Instead, call createRecord with the attributes you would like to set.

I assumed this meant store.createRecord. That's what wasn't working before that's what I found in the guides. Turns out calling createRecord on the model works.

TL;DR:

For me changing

this.store.createRecord('user', ...);

to

App.User.createRecord(...);

solved the problem. Assuming you have defined User as a subclass of DS.Model.

Edit:

This thread: Cannot create record after ember-data upgrade states that the change actually was the other way around. (See: https://github.com/emberjs/data/blob/master/TRANSITION.md as well). It seems like I was using on older version 0.14 (instead of the beta-realease of 1.0) of ember-data. I am using ruby's 'ember-rails'-gem. That might be part of the problem. I'm trying to make sure I get the right version of ember-data now. Like kingpin2k said before...

Second edit:

Yes. Please ignore my inital answer! If you're using the ember-rails gem like me, updating ember-data can be accomplished by:

rails generate ember:install --tag=v1.0.0-beta.4 --ember-data

Otherwise download the newest version manually from the ember website.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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