无法使用RESTSerializer加载数据

时间:2017-04-12 05:23:51

标签: javascript ember.js ember-data

我正在使用RESTSerializer,无法加载数据进行建模。

这是我的代码段。

模型/ sfresult.js:

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

模型/ sfresults1.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'),
  sfresult:  DS.belongsTo('sfresult')
 });

JSON回复:

     { 
     "sfresult":{  
     "message":"SUCCESS",
       "sfresults":[  
           {  
           "viewScore":"100.0",
            "caseNumber":"000005462",
            "id":"1d725883-15f2-4f18-927c-b14455440458",
            "url":"url1",
            "title":"title",
             "description":"",
             "lastModifiedDate":"12/29/16"
            },
           {  
              "caseNumber":"00007082",
               "status":"Closed",
              "id":"b79c0397-f544-45f0-8b91-e2bb7a386ebf",
              "url":"ur2",
               "title":"title1?",
               "description":"",
               "messageId":"500E000000DPA33IAH",
               "lastModifiedDate":"08/16/16"
            }

          ]
         "id":"2b238d70-01ce-4604-913f-29d4c5eeba60"
     }

  }

串行/ sfresult.js

  import RESTSerializer from 'ember-data/serializers/rest';
  import DS from 'ember-data';

  export default RESTSerializer.extend({
  modelNameFromPayloadKey: function(payloadKey) {

    if (payloadKey === 'sfresults') {
      return this._super(payloadKey.replace('sfresults', 'sfresults1'));
    } else {
      return this._super(payloadKey);
    }
  },

  normalizeResponse(store, primaryModelClass, payload, id, requestType) {

   let sfresults1 = payload.sfresult.sfresults;
    let normalizedPayload = {
        sfresults: sfresults1,
        id: payload.sfresult.id,
        message: payload.sfresult.message,
    };

      return this._super(store, primaryModelClass, normalizedPayload, id, 
    requestType);
    },

  });

控制器

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

它正在将数据加载到sfresult1模型中,但没有加载到sfresult.js

失败并出现以下错误。

        Assertion Failed: The response to store.query is expected to be an 
        array but it was a single record. Please wrap your response in an 
        array or use `store.queryRecord` to query for a single record.
   Error
    at assert (http://localhost:8080/assets/vendor.js:16249:13)
    at Object.assert (http://localhost:8080/assets/vendor.js:27921:34)
    at assert (http://localhost:8080/assets/vendor.js:76154:37)
    at http://localhost:8080/assets/vendor.js:86960:41
    at tryCatch (http://localhost:8080/assets/vendor.js:69077:14)
    at invokeCallback (http://localhost:8080/assets/vendor.js:69092:15)
    at publish (http://localhost:8080/assets/vendor.js:69060:9)
    at http://localhost:8080/assets/vendor.js:48963:16
    at invoke (http://localhost:8080/assets/vendor.js:10885:14)
    at Queue.flush (http://localhost:8080/assets/vendor.js:10953:9)

在这几天的时间里,我被震惊了。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

这个错误说的是你的后端正在重新调整Ember Data期望数组的单个记录。建议您切换到使用queryRecord,如果您只想获取一条记录,或者如果您想获得多条记录,则可以修复后端。这有帮助吗?