使用 Ember.js 中的序列化程序支持不同有效负载密钥的最佳方式?
Best way to utilize a serializer in Ember.js to support a different payload key?
当运行下面一行代码:
this.get('store').query('domain-message',
{
domainId: user.defaultDomain.id
});
>>/api/domain-messages?domainId=1
返回此错误:
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.
我在 app/pods/domain-message/model.js 中有一个模型,看起来像这样:
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'),
});
检查此查询的网络时,负载数据如下:
{"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}}
请注意 payloadKey 是如何 "members",这可能会导致问题,因为它可能必须与模型名称相同。有没有办法修改这个payloadKey,改成上面的模型支持?
我没有使用序列化程序,但我知道可能需要这样做才能完成这项工作。谁能建议抑制此错误的最佳途径?生成序列化程序时,它将位于 app/pods/domain-message/serializer.js.
如果您没有明确提供序列化程序,Ember 数据将回退到默认值,即 JSONAPISerializer
. JSONAPISerializer
expects your API to be complaint with JSON:API specification, which isn't the case for the payload included in your question. Therefore you must provide a serializer that can handle your API responses. It shouldn't be that much work, if you extend RESTSerializer
. Please have a look in the Customizing Serializers chapter of Ember Guides 以介绍该主题。
使用自定义序列化程序解决了这个问题。 RESTSerializer 和 JSONSerializer 都适用于这种情况。
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
normalizeQueryResponse(store, ModelClass, payload, id, requestName) {
return this._super(store, ModelClass, payload.messages, id, requestName);
}
});
当运行下面一行代码:
this.get('store').query('domain-message',
{
domainId: user.defaultDomain.id
});
>>/api/domain-messages?domainId=1
返回此错误:
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 usestore.queryRecord
to query for a single record.
我在 app/pods/domain-message/model.js 中有一个模型,看起来像这样:
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'),
});
检查此查询的网络时,负载数据如下:
{"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}}
请注意 payloadKey 是如何 "members",这可能会导致问题,因为它可能必须与模型名称相同。有没有办法修改这个payloadKey,改成上面的模型支持?
我没有使用序列化程序,但我知道可能需要这样做才能完成这项工作。谁能建议抑制此错误的最佳途径?生成序列化程序时,它将位于 app/pods/domain-message/serializer.js.
如果您没有明确提供序列化程序,Ember 数据将回退到默认值,即 JSONAPISerializer
. JSONAPISerializer
expects your API to be complaint with JSON:API specification, which isn't the case for the payload included in your question. Therefore you must provide a serializer that can handle your API responses. It shouldn't be that much work, if you extend RESTSerializer
. Please have a look in the Customizing Serializers chapter of Ember Guides 以介绍该主题。
使用自定义序列化程序解决了这个问题。 RESTSerializer 和 JSONSerializer 都适用于这种情况。
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
normalizeQueryResponse(store, ModelClass, payload, id, requestName) {
return this._super(store, ModelClass, payload.messages, id, requestName);
}
});