在 react/redux 中使用 normalizr 时实体中未定义数组

undefined array in entities while using normalizr in react/redux

我正在使用 React Native 和 Redux。我正在尝试使用 normalizr.

对 API 响应进行规范化。在规范化数据中,数组的键未定义,并且数组外的其他值未显示。

我的代码如下:

架构

import { schema } from "normalizr";

export const transporter_transSchema = new schema.Entity(
    'transporter_trans', {}, {
        idAttribute: "_id"
     }
)
export const paymentSchema = new schema.Entity(
    "payments", {
        transporter_trans: [transporter_transSchema]
    }, 
    { idAttribute: "load_id" }
)

传奇

function* workerGetPaymentDetail(action) {
    const getPaymentDetailResponse = yield call(
        getPaymentDetail,
        action.payload
    );
    let formattedResponse = formatHTTPResponse(getPaymentDetailResponse);
    if (getPaymentDetailResponse.ok) {    
        console.log('FormattedResponse', formattedResponse)
        let normalizedData = normalize(formattedResponse.data, [paymentSchema]);
        console.log('Normalized data', normalizedData);

        yield put({
            type: ADD_ENTITIES,
            payload: normalizedData.entities
        });
        yield put({
            type: paymentAction.GET_PAYMENT_DETAIL_SUCCESS,
            payload: formattedResponse
        })
    } else {
        yield put({
            type: paymentAction.GET_PAYMENT_DETAIL_FAILURE,
            error: formattedResponse
        })
    }
}

API响应

{
    "code": 200,
    "status": "success",
    "message": "OK",
    "data": {
        "_id": 6,
        "load_id": "L1808000000246",
        "payment_amount": 4500,
        "loader_payment": 0,
        "transporter_payment": 0,
        "profit": 0,
        "payment_option": "fulladvance",
        "payment_type": "offline",
        "created_by": 2,
        "status": "pending",
        "transporter_trans": [
            {
                "_id": 7,
                "load_payment_id": 6,
                "load_id": "L1808000000246",
                "user_type": "transporter",
                "payment_type": "online",
                "amount": 510,
                "transaction_status": "Initiated",
                "created_by": 2,
                "status": "pending"
            }
        ],
        "total_payment_amount": 4500,
        "payable_amount": 4500,
        "commission_amount": 238.5,
        "load_key": "L1808000000246"
    }
}

标准化数据

entities:{
    payments:{
        undefined:{
            0:{
                 _id:7
                 amount:510
                 created_by:2
                 intent:"sale"
                 load_id:"L1808000000246"
                 load_payment_id:6
                 payment_id:"ZMSOKPV1533632762178"
                 payment_type:"online"
                 status:"pending"
                 token_type:null
                 transaction_status:"Initiated"
                 user_type:"transporter"
            }
        }
    }
}
result: {[
    0:6
    1:"L1808000000246"
    2:4500
    3:0
    4:0
    5:0
    6:"fulladvance"
    7:"offline"
    8:2
    9:"pending"
    10:undefined
    11:4500
    12:4500
    13:238.5
    14:"L1808000000246"
]}

我已经处理过类似的 API 由数组组成的响应,但在这种情况下找不到问题。

请告诉我我做错了什么

formattedResponse.data 不是一个数组,但您正在对其进行规范化。

改变

let normalizedData = normalize(formattedResponse.data, [paymentSchema]);

let normalizedData = normalize(formattedResponse.data, paymentSchema);