简体   繁体   中英

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')
     });

     export default DS.RESTAdapter.extend({
     host: 'http://localhost:8080',
    namespace: 'server'
   });

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.

Note: if i try without hasMany model it works fine - meaning json response having array of elements and using only sfresults model without sfresult, but wanted to get the message in the JSON response to act accordingly - please ignore this statement if it is confusing a bit.

Any help would be greatly appreciated.

You'll need to make sure you are using the RESTSerializer. By default the serializer in a new app is JSONAPISerializer, which throws the error that you are seeing.

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