gpt4 book ai didi

javascript - Normalizr 不处理信封中的数据

转载 作者:行者123 更新时间:2023-11-30 00:05:32 24 4
gpt4 key购买 nike

我正在对从 Api 接收的数据使用 normalizr。当我收到列表时,我会按预期获得规范化实体。

但是当我发送更新一个实体的请求时,我只收到一个包裹在数据信封中的实体,它没有被 normalizr 正确处理。

// 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) })

结果会是

{
entities: {
players: {
20: {
id: 20,
username: "Gamer"
}
}
},
result: {
players: [
20
]
}
}

2)

normalize(res.data.players[0], player)

结果会是

{
entities: {
players: {
20: {
id: 20,
username: "Gamer"
}
}
},
result: 20
}

webpackbin demo test with other options

关于javascript - Normalizr 不处理信封中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38689567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com