当运行下面一行代码时:
this.get('store').query('domain-message',
{
domainId: user.defaultDomain.id
});
>>/api/domain-messages?domainId=1
将返回以下错误:
对store.query得响应应为数组,但它是单个记录.请将响应 Package 在数组中,或使用store.queryRecord
查询单个记录.错误:Assert失败:对store.query得响应应为数组,但实际上是单个记录.请将响应 Package 在数组中,或使用store.queryRecord
查询单个记录.
我在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/pod/domain-message/serializer.js中。
2条答案
按热度按时间9udxz4iz1#
如果您没有显式提供序列化器,Ember Data将返回到默认值
JSONAPISerializer
。JSONAPISerializer
希望您的API符合JSON:API specification,而您的问题中包含的有效负载却不是这样。因此,您必须提供一个可以处理API响应的序列化器。这不需要太多工作,如果您扩展了RESTSerializer
,请查看Customizing Serializers chapter of Ember Guides中关于该主题的介绍。tnkciper2#
使用自定义序列化程序已解决了此问题。RESTSerializer和JSONSerializer都适用于此情况。