无法在json中打印出属性

时间:2018-06-20 09:07:41

标签: javascript node.js

我正在将一个新的属性(对象)动态添加到现有对象中。

这是我的代码:

.then(transactionDetails => {
            console.log(transactionDetails);
            if(transactionDetails != null){
                transactionDetails["nextDueDate"];
                for(var i = 0; i < transactionDetails.length; i++){
                    if(transactionDetails[i].customers_installment_detail.installment1_isPaid === false){
                        nextDueDate = {
                            installment1_dueAmount:transactionDetails[i].customers_installment_detail.installment1_dueAmount,
                            installment1_dueDate:transactionDetails[i].customers_installment_detail.installment1_dueDate
                        }
                    }else if(transactionDetails[i].customers_installment_detail.installment2_isPaid === false){
                        nextDueDate = {
                            installment2_dueAmount:transactionDetails[i].customers_installment_detail.installment2_dueAmount,
                            installment2_dueDate:transactionDetails[i].customers_installment_detail.installment2_dueDate
                        }
                    }else if(transactionDetails[i].customers_installment_detail.installment3_isPaid === false){
                        nextDueDate = {
                            installment3_dueAmount:transactionDetails[i].customers_installment_detail.installment3_dueAmount,
                            installment3_dueDate:transactionDetails[i].customers_installment_detail.installment3_dueDate
                        }
                    }else if(transactionDetails[i].customers_installment_detail.installment4_isPaid === false){
                        nextDueDate = {
                            installment2_dueAmount:transactionDetails[i].customers_installment_detail.installment4_dueAmount,
                            installment2_dueDate:transactionDetails[i].customers_installment_detail.installment4_dueDate
                        }
                    }
                    transactionDetails[i]["nextDueDate"] = nextDueDate;
                }
                res.json({response:transactionDetails});
            }else{
                res.json({response:"No active transaction."});
            }
        })

我正在动态添加nextDueDate。我可以通过键入transactionDetails[0].nextDueDate在控制台中打印出该值,但是当我尝试用JSON打印出整个对象时,nextDueDate丢失了,无法打印出来。有什么原因吗?

1 个答案:

答案 0 :(得分:0)

这是由于根据此线程出现了Sequelize问题: Add Property to Object that is returned by Sequelize FindOne

我通过在模型中添加虚拟列来解决此问题。

if (localStorage.getItem('mykey') === 'null') {
  console.log('It is null');
}