[英]Why do I keep getting null in this ternary assignment?
我具有以下功能:
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) => {
responseData.map(function (v) {
v.orderStatusChange = v.order ? v.order.orderStatusChange : null })
this.setState({orders:responseData})
console.log("Log del responseData");
console.log(responseData);
})
.catch(function() {
console.log("error");
});
}
我得到了带有responseData的对象数组,看起来像THIS 。 我想访问orderStatusChange并获取状态。 我已经能够从数组中获取其他值,例如订单的id或创建的日期,将数组的键直接放在必须打印它们的表中:
const HISTORY_TABLE_COLUMNS = [
{
key: '_id',
label: 'Número de pedido'
}, {
key: 'created',
label: 'Fecha del pedido'
}, {
key: 'status',
label: 'Estado'
}
];
现在看起来像这样: TABLE
但是由于状态在主数组中的另一个数组中,所以我认为我可以将其映射为如功能所示:
responseData.map(function (v) {
v.orderStatusChange = v.order ? v.order.orderStatusChange : null })
但是orderStatusChange不断获得null作为值。
如何获得状态值?
编辑:添加Json对象的一部分而不是捕获:
(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
查看您的json,您的对象中没有order
属性。 这就是为什么您总是进入三元运算符的false
部分的原因。
与您的问题无关,但是没有任何使用者遵循.map
看起来很没用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.