Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assertion Failed: You must include an 'id' for account in an object passed to 'push' Ember.js v-2.4

I'm new to Ember and I can't find anywhere a solution to my problem. I have read the questions here in stack and in other ember forums, but none of them seems to work for me.

I'm trying to create a simple signup form. I should note that for the backend I use django. Here is my code:

Server Response:

[{"username":"user1","password":"123","email":"[email protected]"},        
{"username":"user2","password":"456","email":"[email protected]"}]

Ember Model:

import DS from 'ember-data';

export default DS.Model.extend({
    username: DS.attr(),
    password: DS.attr(), 
    email: DS.attr()
});

Ember Adapter: import DS from 'ember-data';

export default DS.RESTAdapter.extend({
    host: '/api',
    contentType: 'application/json',
    dataType: 'json',

    headers: {
        username: 'XXXX',
        password: 'XXXX'
   } 
});

Ember Serializer:

import DS from 'ember-data';

export default DS.JSONSerializer.extend({
    primaryKey: '_id'
});

Ember Route: import Ember from 'ember';

export default Ember.Route.extend({
    model() {
        return this.store.findAll('account');
    }
});

Ember Controller:

import Ember from 'ember';

export default Ember.Controller.extend({

    actions: {

    signup(){
        console.log('My username is: ', this.get('username'));
        console.log('My password is: ', this.get('password'));
        console.log('My email is: ', this.get('email'));

        var account = this.store.createRecord('account', {
                username: this.get('username'),
                password: this.get('password'),
                email: this.get('email')
        });

        account.save();

    }
    }
});

With this implementation I get the aforementioned error. Any help would be appreciated. Thank you in advance.

like image 320
Jack Avatar asked Mar 22 '16 13:03

Jack


2 Answers

Your backend should respond with an id, i.e

{"id":123,"username":"user1","password":"123","email":"[email protected]"}

You don't really need to use the serializer, unless you want a specific field to act as an id. So in the example above, ember-data would expect your backend to return

{**"_id":123**,"username":"user1","password":"123","email":"[email protected]"}

Or you could do something like this:

import DS from 'ember-data';

export default DS.JSONSerializer.extend({
    primaryKey: 'username'
});

And use your username as an id.

like image 72
Igor Avatar answered Sep 22 '22 23:09

Igor


I fixed my error. The problem was that the backend was using username as id so ember could not recognized it.

The fixed code for the Ember Serializer is:

import DS from 'ember-data';

export default DS.JSONSerializer.extend({
     primaryKey: 'username'
});
like image 26
Jack Avatar answered Sep 19 '22 23:09

Jack