Normalizr 模式:嵌套实体不会规范化
Normalizr schema : nested entities does not become normalized
我有一个实体 "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
.
您的productSchema
不正确。应该明确说明term_attributes
是一个数组,有两种方式:
使用 Array
shorthand:
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)
});
我有一个实体 "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
.
您的productSchema
不正确。应该明确说明term_attributes
是一个数组,有两种方式:
使用 Array
shorthand:
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)
});