我正在使用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)


几天来,我在这里感到震惊。任何帮助将不胜感激。

最佳答案

该错误的意思是您的后端正在重新调整Ember Data期望一个数组的单个记录。如果您只想获得一条记录,建议您使用queryRecord;如果要获得多条记录,则建议修复后端。有帮助吗?

09-25 19:05