2017-04-12 93 views
0

我正在使用RESTSerializer,無法加載到要建模的數據。無法使用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

這個錯誤是說什麼是你的後端正在重新調整一個單一的記錄,其中Ember數據期待一個數組。如果您想要獲取一條記錄,或者如果您想獲取多條記錄,請修復後端,建議您切換至使用queryRecord。這有幫助嗎?