我想规范化从API收到的响应。典型的响应可能如下所示:
// Get all projects
{data:[
{
id: 1
...
team:{
data: {
id:15
...
}
}
},
{
id:2,
....
},
{
id:3,
...
}
]}
如何编写模式以便删除“数据”容器? 目前,我的架构如下:
export const project = new schema.Entity('projects', {
team: team, // team omitted
},
{
processStrategy: (value, parent, key) => parent.data
}
)
export const arrayOfProjects = new schema.Array(project)
我正在使用它:
const normalizedProjects = normalize(jsonResponse, arrayOfProjects)
normalizedProjects 然后如下所示:
{
entities:{
projects:{
undefined:{
0:{
team:{
data:{
id:15,
...
}
}
},
1:{...},
2:{...}.
...
50:{...},
}
}
},
result:[] // length is 0
}
我不确定为什么项目列表包含在'undefined'中?
答案 0 :(得分:1)
我也使用json_api架构。 这样怎么样?
const projectsSchema = new schema.Entity('projects', {}, {
processStrategy: processStrategy
});
export const processStrategy = (value, parent, key) => {
const attr = value.attributes;
delete value.attributes;
return { ...value, ...attr };
};
export const fetchProjectsSchema = {
data: [projectsSchema]
}
答案 1 :(得分:0)
您希望省略stupidPolicy :: Policy
stupidPolicy = Policy check where
check Public = Edit
check Contributor = View
check Owner = None
的每个实体架构(或其他任何基础知识已更改)都需要包含您编写的processStrategy以删除或更改任何数据。 (参见tests)