Normalizr 不处理信封中的数据

Normalizr is not processing data in envelope

我正在对从 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