我有限定的本狀態:無法從JSON對象內部的陣列訪問值在Reactjs
constructor(props){
super(props);
this.state = {
open: false,
customers:[],
customer:{},
products:[],
product:{},
orders:[],
order:{},
newForm:true,
phoneNumbererror:null,
shop:this.props.salon,
value:'a',
showTab:'none',
slideIndex: 0,
};
}
與含有取下面的函數,我收到與responseData對象的數組。
getHistory(){
console.log("Log antes del fetch de customer id");
console.log(this.state.customer._id);
fetch(
DOMAIN+'/api/orders/customer/'+this.state.customer._id, {
method: 'get',
dataType: 'json',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization':'Bearer '+this.props.token
}
})
.then((response) =>
{
return response.json();
})
.then((responseData) => {
this.setState({orders:responseData})
console.log("Log del responseData");
console.log(responseData);
})
.catch(function() {
console.log("error");
});
}
該功能被稱爲在handleCellClick
,其中I通過從消費者的一些數據,如ID:
handleCellClick(y,x,row){
this.setState({
open:true,
slideIndex: 0,
newForm:false,
customer:{...row}
});
this.getProfiles();
this.getHistory();
}
從得到的JSON對象的獲取,並保持它內this.state.orders
看起來像這樣:對象的
(29) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0:
created:"2017-07-06T15:58:07.958Z"
customer:"59561f3f1d178e1966142ad7"
lastModified:"2017-07-06T15:58:07.958Z"
orderList:[]
orderStatusChange:Array(1)
0:{status: "5", comments: "Creado en back antes de pagar", _id: "595e5e0f60fbf65149916b7c", created: "2017-07-06T15:58:07.958Z"}
length:1
__proto__:Array(0)
shop:"59108159bc3fc645704ba508"
totalAmount:4000
__v:0
_id:"595e5e0f60fbf65149916b7b"
__proto__:Object
捕捉更好地理解:CAPTURE
正如取,用這條線this.setState({orders:responseData})
我可以通過orders
到我想要的ID表,可以顯示日期和狀態前面所示:
<DataTables
height={'auto'}
selectable={false}
showRowHover={true}
columns={HISTORY_TABLE_COLUMNS}
data={this.state.orders}
showCheckboxes={false}
rowSizeLabel="Filas por página"
/>
稱爲表是:
const HISTORY_TABLE_COLUMNS = [
{
key: '_id',
label: 'Número de pedido'
}, {
key: 'created',
label: 'Fecha del pedido'
}, {
key: 'status',
label: 'Estado'
}
];
的問題涉及到,當我想打印_id
,created
和status
,前兩個值被印刷沒有任何問題,但status
是陣列orderStatusChange
內,我無法打印。
CAPTURE OF THE CURRENT SITUATION
如何訪問status
打印它放在桌子上?
如何從'state.orders'中訪問'status'? –
長度爲1的數組orderStatusChange總是? – Dev
現在我只是把'status'這個關鍵字放在表上(這不起作用),但是可以用'_id'和'created'正常工作。 – Calicorio2