Reputation: 13
I am using the RESTSerializer and could not able to load to the data to model.
here is my code snippet.
model/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')
});
model/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 response :
{
"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"
}
}
serializer/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);
},
});
controller
let sfdata = this.store.query('sfresult',{ 'searchText':
inputSearchText, 'searchType' : 'SF' } );
It is loading the data into sfresult1 model but not loading into sfresult.js
it fails with following error.
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)
I am struck here at this point of couple of days. any help would be greatly appreciated.
Upvotes: 0
Views: 125
Reputation: 5955
What that error is saying is your backend is retuning a single record where Ember Data is expecting an array. It is suggesting that you switch to using queryRecord
if you want to only get one record or you fix your backend if you want to get multiple records. Does that help?
Upvotes: 1