我有一个实体“term_attributes”,两个嵌套实体没有被标准化。我使用normalizr:“^ 3.2.2”。
原始数据是一系列产品,这里是相关位:
[{
"id": 9,
"price": "184.90",
"term_attributes": [
{
"id": 98,
"attribute": {
"id": 1,
"name": "Color",
"slug": "color"
},
"term": {
"id": 94,
"name": "Bags",
"slug": "bags"
}
},
规范化代码:
export const termSchema = new schema.Entity('terms');
export const attributeSchema = new schema.Entity('attributes');
export const termAttributeSchema = new schema.Entity('term_attributes', { idAttribute: 'id'},{
attribute: attributeSchema,
term: termSchema
});
export const termAttributeListSchema = new schema.Array(termAttributeSchema);
export const productSchema = new schema.Entity('products', {
term_attributes: termAttributeListSchema,
});
编辑:忘记添加productListSchema(虽然不重要):
export const productListSchema = new schema.Array(productSchema);
Term_attributes已标准化,但不是其嵌套实体(属性和条款)。结果如下:
{
"entities": {
"27": {
"id": 27,
"price": "184.90",
"term_attributes": [105, 545, 547, 2, 771]
},
"term_attributes": {
"2": {
"id": 2,
"attribute": {
"id": 1,
"name": "Color",
"slug": "color"
},
"term": {
"id": 2,
"name": "Fashion",
"slug": "fashion"
}
},
如果我从termAttributeSchema中删除“idAttribute”,则规范化失败:
export const termAttributeSchema = new schema.Entity('term_attributes', {
attribute: attributeSchema,
term: termSchema
});
^^这里有什么问题?
更新:
下面的Paul Armstrongs解决方案有效,我只是跳过termAttributeListSchema
而是使用termAttributeSchema:term_attributes: termAttributeSchema
。
答案 0 :(得分:1)
您的productSchema
不正确。它应明确声明term_attributes
是一个数组,可以通过两种方式完成:
使用Array
简写:
export const productSchema = new schema.Entity('products', {
term_attributes: [termAttributeListSchema]
});
或使用schema.Array
:
export const productSchema = new schema.Entity('products', {
term_attributes: new schema.Array(termAttributeListSchema)
});