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

时间:2018-08-14 15:23:24

标签: react-native react-redux redux-saga normalizr

我正在使用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响应,其中包括介于两者之间的数组,但是在这种情况下找不到问题。

请让我知道我在做什么错

1 个答案:

答案 0 :(得分:0)

script2.js不是数组,但是您正在将其标准化。

更改

formattedResponse.data

收件人

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