Javascript Normalizer未处理信封中的数据
我使用normalizer处理从Api接收的数据。当我收到列表时,我得到了预期的规范化实体 但当我发送更新一个实体的请求时,我只接收到一个封装在数据信封中的实体,该数据信封没有被normalizer正确处理Javascript Normalizer未处理信封中的数据,javascript,redux,normalizr,Javascript,Redux,Normalizr,我使用normalizer处理从Api接收的数据。当我收到列表时,我得到了预期的规范化实体 但当我发送更新一个实体的请求时,我只接收到一个封装在数据信封中的实体,该数据信封没有被normalizer正确处理 // Normalize setting import { Schema, arrayOf } from 'normalizr' export const player = new Schema('players') export const arrayOfPlayers = arrayO
// Normalize setting
import { Schema, arrayOf } from 'normalizr'
export const player = new Schema('players')
export const arrayOfPlayers = arrayOf(player)
//This is what I use for normalize
response => normalize(response, {players: schema.player})
我收到的数据如下,列表中只有一名玩家:
{
code: 200,
message: '',
data: {
players: [{
id: 20
username: 'Gamer'
}]
}
}
我应该改变什么来检索作为规范化实体的播放器
更新:
Api调用的示例
fetch(
url,
{ credentials: 'include' }
)
.then(checkStatus)
.then(response => response.json())
.then(response => normalize(response, {players: schema.player}))
.then( // dispatch redux action )
.catch(function (error) {
})
如果收到数组,则应使用arrayOf,否则通过res.data.players[0]引用对象。 改变这个
normalize(response, {players: schema.player} )
致:
1)
normalize(response.data, {players: arrayOf(schema.player) })
normalize(res.data.players[0], player)
结果将是
{
entities: {
players: {
20: {
id: 20,
username: "Gamer"
}
}
},
result: {
players: [
20
]
}
}
{
entities: {
players: {
20: {
id: 20,
username: "Gamer"
}
}
},
result: 20
}
2)
normalize(response.data, {players: arrayOf(schema.player) })
normalize(res.data.players[0], player)
结果将是
{
entities: {
players: {
20: {
id: 20,
username: "Gamer"
}
}
},
result: {
players: [
20
]
}
}
{
entities: {
players: {
20: {
id: 20,
username: "Gamer"
}
}
},
result: 20
}
您能提供一个api调用的示例吗?未经教育的猜测:
normalize(response.data…)
或normalize(response,{data:{players:}})
?感谢我使用了解决方案1),但有一些小的改动,这可能是api的糟糕设计。但我要寻找的响应是索引0上的对象。normalize(response[0].data,{players:arrayOf(schema.player)})后端端点出现问题,现在我可以使用normalize(response.data,{players:schema.player})