我有一个实体“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,
});
edit :忘了添加productListSchema(虽然不重要):
export const productListSchema = new schema.Array(productSchema);
Term_attributes是规范化的,但不是其嵌套实体( attributes 和 terms ) . 结果如下:
{
"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
});
这个怎么了?
Update :
下面的保罗·阿姆斯特朗解决方案有效,我只是跳过 termAttributeListSchema
而是使用termAttributeSchema: term_attributes: termAttributeSchema
.
1 回答
你的
productSchema
不正确 . 它应该明确声明term_attributes
是一个数组,可以通过两种方式完成:使用
Array
速记:或者使用
schema.Array
: