mardi 31 janvier 2017

this.store.query fails with "The response to store.query is expected to be an array but it was a single record"

I am trying build a small search application with Ember.JS - when I try to do REST call and try to display in the UI it fails with error message as "The response to store.query is expected to be an array but it was a single record. Please wrap your response in an array"

Here is my code Snippet:

  1. application/adapter.js

      import DS from 'ember-data';
      export default DS.RESTAdapter.extend({
      host: 'http://c6403:2222',
      namespace: 'server'
     });
    
    
  2. searializers/results.js

    import DS from 'ember-data';
    export default DS.RESTSerializer.extend({
    });
    
    

tried

    import DS from 'ember-data';

    export default DS.RESTSerializer.extend({
    normalizePayload: function(payload) {
      return {
        title: payload.title,
        description: payload.description,
        type: payload.type,
        url: payload.url,
       };
      }
     });

  1. My router/index.js

      import Ember from 'ember';
      export default Ember.Route.extend({
       model: function (){
    
           var searchtext = "Ambariserver-default-port";
           return this.store.query('results',{ searchtext }         ).then(function(results) {
       alert(results);
      return results;
      });
            } }
    
    
  2. model/results.js

      import DS from 'ember-data';
       export default  DS.Model.extend({
        title: DS.attr('string'),
         description: DS.attr('string'),
         url: DS.attr('string'),
         type: DS.attr('string'),
         searchText: DS.attr('string')
       });
    
    
  3. My JSON response:

       {
     "results": [
        {
         "url": "http://ift.tt/IDrjcF",
         "title": "This is SF KB",
         "description": "This is SF HCC",
         "type": "SF"
           },
        {
        "url": "http://ift.tt/QIus9W",
        "title": "This is SF JIRA",
        "description": "This is SF HCC",
        "type": "JIRA"
        }
     ]
    }
    
    

I am struck here for more than 3 days and need help to resolve this issue. Can you please advise what is wrong with my code. does Adapter and serializer looks fine?




Aucun commentaire:

Enregistrer un commentaire