mercredi 7 août 2019

Best way to utilize a serializer in Ember.js to support a different payload key?

When running the following line of code:

this.get('store').query('domain-message', 
{
    domainId: user.defaultDomain.id
});

>>/api/domain-messages?domainId=1

this error is returned:

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: 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.

I have a model in app/pods/domain-message/model.js that looks like so:

import DS from 'ember-data';
import attr from 'ember-data/attr';

export default DS.Model.extend({
    campaignId: attr('number'),
    orgId: attr('number'),
    orgName: attr('string'),
    userId: attr('number'),
    memberId: attr('number'),
    messageSubject: attr('string'),
    messageBody: attr('string'),
    attachmentLink: attr('string'),
    //sentTimestamp: attr('date'),
    //receivedTimestamp: attr('date'),
    //archivedTimestamp: attr('date'),
    messageSeen: attr('boolean'),
    messageRead: attr('boolean'),
    parentMessageId: attr('number'),
    actionCount: attr('number'),
    completedActionCount: attr('number'),
    //actionStatusUpdatedTimestamp: attr('date'),
});

When inspecting the network for this query, the payload data is as follows:

{"messages":[{"id":1,"campaignId":1,"orgId":1,"orgName":"TestOrg","userId":120,"memberId":"12345","messageSubject":"Test Message","messageBody":"test test test","attachmentLink":"https://google.com","sentTimestamp":null,"receivedTimestamp":"2019-08-07T17:01:39Z","archivedTimestamp":null,"messageSeen":false,"messageRead":false,"parentMessageId":0,"actionCount":0,"completedActionCount":0,"actionStatusUpdatedTimestamp":"2019-08-07T17:01:39Z"},{"id":2,"campaignId":1,"orgId":1,"orgName":"TestOrg2","userId":120,"memberId":"12349","messageSubject":"Hello","messageBody":"How are you?","attachmentLink":"https://google.com","sentTimestamp":null,"receivedTimestamp":"2019-08-07T17:07:39Z","archivedTimestamp":null,"messageSeen":false,"messageRead":false,"parentMessageId":0,"actionCount":0,"completedActionCount":0,"actionStatusUpdatedTimestamp":"2019-08-07T17:07:39Z"}],"meta":{"count":2,"total":2,"offset":null,"limit":null}}

Notice how the payloadKey is "members", this may be causing the issue since it may have to be the same as the models name. Is there anyway to modify this payloadKey and change it so that the model above supports it?

I am not using a serializer, but I do know that it may be required to do so to make this work. Can anyone suggest the best route to suppress this error? When generating a serializer, it will be located in app/pods/domain-message/serializer.js.

Thanks in advanced.




Aucun commentaire:

Enregistrer un commentaire