简体   繁体   English

为什么在此三元分配中我总是保持空值?

[英]Why do I keep getting null in this ternary assignment?

I have this function with a fetch: 我具有以下功能:

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");
            });
    }

I get an array of objects with responseData, which looks like THIS . 我得到了带有responseData的对象数组,看起来像THIS I want to access orderStatusChange and get the status. 我想访问orderStatusChange并获取状态。 I've been able to get other values from the array like the id of the order or the date that was created putting the key of the array directly in the table where they have to be printed: 我已经能够从数组中获取其他值,例如订单的id或创建的日期,将数组的键直接放在必须打印它们的表中:

const HISTORY_TABLE_COLUMNS = [
    {
        key: '_id',
        label: 'Número de pedido'
    }, {
        key: 'created',
        label: 'Fecha del pedido'
    }, {
        key: 'status',
        label: 'Estado'
    }
]; 

Looking like this now: TABLE 现在看起来像这样: TABLE

But since status is inside another array within the main array, I thought that I could map it to get it as shown in the function: 但是由于状态在主数组中的另一个数组中,所以我认为我可以将其映射为如功能所示:

responseData.map(function (v) {
                v.orderStatusChange = v.order ? v.order.orderStatusChange : null  })

But orderStatusChange keeps getting null as value. 但是orderStatusChange不断获得null作为值。

How could I get the status value? 如何获得状态值?

EDIT: Adding part of the Json object instead of a capture: 编辑:添加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

Looking at your json there is no order property in your object. 查看您的json,您的对象中没有order属性。 This is why you always go in the false section of the ternary operator. 这就是为什么您总是进入三元运算符的false部分的原因。

Not related to your question, but having a .map not being followed by any consumers look pretty useless. 与您的问题无关,但是没有任何使用者遵循.map看起来很没用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM