Polymorphic relations in embedded relations

I have an ember cli project and I'm trying to replicate a simple script in which I have a Post model that hasMany Comments . This ratio is polymorphic. I have two types of Body Comment and Title Comment.

// app/models/post.js
import DS from 'ember-data';

export default DS.Model.extend({
    entry:          DS.attr('string'),
    comments:       DS.hasMany('comment', {polymorphic: true})
});


// app/models/comment.js
import DS from 'ember-data';

export default DS.Model.extend({
    text: DS.attr('string'),
    post: DS.belongsTo('post')
});

// app/models/body.js
import DS from 'ember-data';
import Comment from './comment';

export default Comment.extend({
    body: DS.attr('string')
});

// app/models/title.js
import DS from 'ember-data';
import Comment from './comment';

export default Comment.extend({
    title: DS.attr('string')
});

I have a serializer for the Post model

import DS from 'ember-data';
export default DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    comments: {
      embedded: 'always'
    }
  }
});

JSON returned by the server in GET / posts / 1 ,

{
    "posts": {
        "id": "1",
        "entry": "This is first post",
        "comments": [
            {
                "id": "1",
                "post": "1",
                "type": "body",
                "text": "This is the first comment on first post",
                "body": "This is a body comment"
            },
            {
                "id": "2",
                "post": "1",
                "type": "title",
                "text": "This is the second comment on first post",
                "title": "This is a title comment"
            }
        ]
    }
}

But the Ember data does not allow deserializing comments with the following error:

Error while processing route: index Cannot read property 'typeKey' of undefined TypeError: Cannot read property 'typeKey' of undefined
    at Ember.Object.extend.modelFor (http://localhost:4200/assets/vendor.js:71619:22)
    at Ember.Object.extend.recordForId (http://localhost:4200/assets/vendor.js:71074:25)
    at deserializeRecordId (http://localhost:4200/assets/vendor.js:72099:27)
    at deserializeRecordIds (http://localhost:4200/assets/vendor.js:72116:9)
    at http://localhost:4200/assets/vendor.js:72081:11
    at http://localhost:4200/assets/vendor.js:70135:20
    at http://localhost:4200/assets/vendor.js:17687:20
    at Object.OrderedSet.forEach (http://localhost:4200/assets/vendor.js:17530:14)
    at Object.Map.forEach (http://localhost:4200/assets/vendor.js:17685:14)
    at Function.Model.reopenClass.eachRelationship (http://localhost:4200/assets/vendor.js:70134:42) 

This happens when the following code is executed:

import Ember from 'ember';

export default Ember.Route.extend({
    model: function() {
        return this.store.find('post', 1);
    },

    setupController: function(controller, model) {
        console.log("Post entry: " + model.get('entry'));
        var comments = model.get('comments');
        comments.forEach(function(comment){
            console.log("Comment: " + comment.get('text'));
            console.log(typeof comment);
            //console.log("Comment Body " + comment.get('body'));
            //console.log("Comment Title " + comment.get('title'));
        });
    }
});

Please help me understand that I am doing something wrong, and if so, what is the correct way to solve this requirement.

+4
1

, @Dipayan, - , Ember Data HasMany Embedded Polymorphic. hasMany . TypeError: Cannot read property 'typeKey' of undefined, , .

, , , , , , Ember.

+2

All Articles