我试图访问从json / api返回的对象。
当我到达对象时,返回是正常的,但是当我到达某个对象项时,模拟器会显示错误。
我的代码有效,返回完整的对象:
render() {
const boleto = this.props.boletodetalhe.data.unico;
console.tron.log(boleto);
return (
<View style={styles.container}>
</View>
);
}
}
如果我更改了我的代码,将我的对象项目放在控制台上会产生错误。
console.tron.log(boleto.nome);
这是我的电话。
import api from 'services/api';
import { call, put } from 'redux-saga/effects';
import global from 'services/global';
import { Creators as boletodetalheActions } from 'store/ducks/boletodetalhe';
export function* BoletodetalheRequest(action) {
console.tron.log('PAYLOAD');
console.tron.log(action);
try {
const response = yield call(api.get, `Boleto/GeraBoletoFinal?code=${global.key}&IdBoleto=${action.payload.boleto.id}&IdSacado=${action.payload.sacado.id}`);
if (!response.data.erro) {
yield put(boletodetalheActions.boletodetalheSuccess(response.data));
} else {
yield put(boletodetalheActions.boletodetalheError(response.data.mensagem));
}
} catch (err) {
yield put(boletodetalheActions.boletodetalheError('Chamada inexistente'));
}
}
这是我的减速机。
export const Types = {
ADD_REQUEST: 'boletodetalhe/ADD_REQUEST',
ADD_SUCCESS: 'boletodetalhe/ADD_SUCCESS',
ADD_FAILURE: 'boletodetalhe/ADD_FAILURE',
};
const initialState = {
data: [],
loading: false,
errorOnAdd: null,
carregou: false,
};
export default function boletodetalhe(state = initialState, action) {
switch (action.type) {
case Types.ADD_REQUEST:
return { ...state, loading: true };
case Types.ADD_SUCCESS:
return {
...state,
data: action.payload.Data,
loading: false,
errorOnAdd: null,
carregou: true,
modal: true,
};
case Types.ADD_FAILURE:
return {
...state,
loading: false,
errorOnAdd: action.payload.message,
};
default:
return state;
}
}
export const Creators = {
boletodetalheRequest: (user, boleto, sacado) => ({
type: Types.ADD_REQUEST,
payload: {
user,
boleto,
sacado,
},
}),
boletodetalheSuccess: Data => ({
type: Types.ADD_SUCCESS,
payload: {
Data,
},
}),
boletodetalheError: message => ({
type: Types.ADD_FAILURE,
payload: {
message,
},
}),
};
答案 0 :(得分:0)
请将数据初始化为对象而不是数组。改变这个
const initialState = {
data: {}, // Change this line, from [] -> {}
loading: false,
errorOnAdd: null,
carregou: false,
};