【发布时间】:2016-12-14 06:15:33
【问题描述】:
我正在使用 Normalizr 从不同的 API 端点获得相同的响应形状。
const post = new Schema('posts');
const posts = arrayOf('post');
const listResponse = [
{id: 1, text: 'post one'},
{id: 2, text: 'post two'},
];
normalize(listResponse, posts);
/*
{
entities: {
posts: {
1: {id: 1, text: 'post one'},
2: {id: 2, text: 'post two'}
}
},
result: [1, 2]
}
*/
const singleResponse = {id: 1, text: 'post one'};
normalize(singleResponse, post);
/*
{
entities: {
posts: {
1: {id: 1, text: 'post one'}
}
},
result: 1
}
*/
那么我想不管它是如何产生的,我都会处理标准化的反应。
但问题是,对于单个项目,我得到的是 result: 1 而不是数组 result: [1],这会导致我以后的代码出现一些问题。
现在我必须手动将result 标准化为数组,但也许有更好的方法来做到这一点?
【问题讨论】: