我正在使用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响应,其中包括介于两者之间的数组,但是在这种情况下找不到问题。
请让我知道我在做什么错
答案 0 :(得分:0)
script2.js
不是数组,但是您正在将其标准化。
更改
formattedResponse.data
收件人
let normalizedData = normalize(formattedResponse.data, [paymentSchema]);