I get “Assertion failed: You must include an `id`

2019-06-24 23:41发布

I must have read every question on stack overflow regarding my issue and have not found a solution. I am very new to Ember and Node so please bear with me.

The server responds with this format:

{
"_id" : "53fddf59d72f9b4d3a3e164a"
"about" : [
    {"from" : "foo"},
    {"text" : "bar"},
    ... ]
}

My model looks like this:

App.About = DS.Model.extend({
    from : DS.attr('string'),
    text : DS.attr('string'),
    ...
}

Adapter & serializer:

App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://localhost:3000'
});

App.ApplicationSerializer = DS.JSONSerializer.extend({
    primaryKey: '_id'
});

And my route:

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

Using handlebars:

<script type="text/x-handlebars" data-template-name="about">
    <p>{{from}}</p>
</script>

The route will render, but the console gives the error:

DEBUG: ------------------------------- ember.js:3285
DEBUG: Ember : 1.3.0 ember.js:3285
DEBUG: Ember Data : 1.0.0-beta.5 ember.js:3285
DEBUG: Handlebars : 1.3.0 ember.js:3285
DEBUG: jQuery : 1.10.2 ember.js:3285
DEBUG: ------------------------------- ember.js:3285
generated -> route:application Object {fullName: "route:application"} ember.js:3285
Assertion failed: You must include an id in a hash passed to push ember.js:3285
generated -> controller:about Object {fullName: "controller:about"} ember.js:3285
Transitioned into 'about'

What am I doing wrong?!


Update:

Have accepted Kingpin2k's answer even though this was not the exact format I ended up using. See the comments for details.

1条回答
爷的心禁止访问
2楼-- · 2019-06-25 00:34

Your format should have the type plural, and the id on the individual records.

 {
  "abouts" : [
    {
     "from" : "foo",
     "text" : "bar",
     "_id" : "53fddf59d72f9b4d3a3e164a"
    },
    {
     "from" : "foo2",
     "text" : "bar2",
     "_id" : "53fddf59d72f9b4d3a3e164k"
    },
    ... ]
 }
查看更多
登录 后发表回答