Api から受け取るデータに normalizr を使用しています。リストを受け取ると、正規化されたエンティティが期待どおりに取得されます。
しかし、1 つのエンティティを更新するリクエストを送信すると、データ エンベロープでラップされたエンティティが 1 つしか受信されず、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})
そして、リスト内のプレーヤーが 1 つだけの場合、次のようなデータを受け取ります。
{
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) {
})