samedi 8 avril 2017

Error while trying to load ember data model from REST API

Version: Ember 2.11

I am trying to use Ember data model to load the data from REST API but it is failing with error message as "ember.debug.js:17634 TypeError: Cannot read property 'type' of undefined at Class._pushInternalModel (store.js:2005)

Here is more details: 1. REST API response is:

     {  
      "sfresults":[  
              {  
               "url":"https://google.com/1",
               "title":"Titl1",
               "description":"Description1",
              "type":"KB",
              "lastModifiedDate":"12/23/16",
              "viewScore":"86.12006476690622",
              "caseNumber":"case1",
              "id":"cd4ac3e8-c3ac-4be5-ad11-c62a85ddf289"
             },
            {  
               "url":"https://google.com/2",
               "title":"Titl2",
               "description":"Description2",
               "type":"KB",
               "lastModifiedDate":"12/23/16",
                "viewScore":"86.12006476690622",
               "caseNumber":"case2",
              "id":"cd4ac3e8-c3ac-4be5-ad11-c62a85ddf289"
           },
        ],
    "message":"SUCCESS",
   "id":"3bd116c7-db63-4277-8ace-a7ea846a04ee"
  }

Controller code:

          let sfdata = this.store.query('sfresult',{ 'searchText': inputSearchText, 'searchType' : 'SF' } );
           this.set('sfresult', sfdata);

My Models

 sfresult.js

      import DS from 'ember-data';
      export default  DS.Model.extend({
      sfresults: DS.hasMany('sfresults'),
      message: DS.attr('string')
     });

sfresults.js
       import DS from 'ember-data';

       export default  DS.Model.extend({

       title: DS.attr('string'),
       description: DS.attr('string'),
       caseNumber: DS.attr('string'),
       lastModifiedDate: DS.attr('string'),
       type: DS.attr('string'),
       url: DS.attr('string'),
       searchText: DS.attr('string'),
       messageId: DS.attr('string')
     });

Then i wanted to iterate sfresult and show it in the UI. but here as soon as API call response comes back it is unable to load into ember data models. fails with above error.

Any help would be greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire