我正在使用react + redux,我正在尝试规范化API的返回。我的代码如下:
架构是:
import { schema} from "normalizr";
export const walkthroughSchema = new schema.Entity("walkthroughs");
export const walkthroughListSchema = [walkthroughSchema];
我的抓取是:
static listAllWalkthroughs() {
return window
.fetch("http://127.0.0.1:3000/walkthroughs")
.then(response => {
return response.json();
})
.then(jsonData => {
const normalized = normalize(jsonData, schemas.walkthroughListSchema );
console.log("api - response", jsonData);
console.log("api - normalized dsata ", normalized);
});
}
获取的响应是:
[0 : {
name: "name 1",
ID: "5aa7317906ddabf5f7d76c58",
username: "",
url: "https://google.com", status: "", …},
1 : {
name: "name 2",
ID: "5aa7346706ddabf5f7d76d5d",
username: "",
url: "https://google.com",
status: "", …},
2 : {
name: "name 3",
ID: "5acd9740d53edd2200818bb1",
username: "",
url: "https://google.com",
status: "", …}]
当我打印出结果的标准化数据时:
entities:
walkthoroughs: {
undefined: {
id : "5acd9740d53edd2200818bb1",name:
"name 3",
status: "",
steps: [],
url: "https://google.com",
username:""
}
}
result: {[
0: undefined,
1: undefined,
2: undefined
]
}
问题是,我做错了什么?