如何在javascript中访问空对象中的嵌套对象

时间:2016-12-03 10:33:00

标签: javascript node.js

//这是模特。我想访问merchant_reference_number。依赖于此我想将settlement_status更新为true。

    body: {
       code: {type: String},
       message : {type: String},
       status_code: {type: String},
       yes_reference_number: {type: String},
       merchant_reference_number: {type: String},
       amount:{type: Number},
       status: {type: String},
       error: {type: String}

    },

    settlement_status: {type: Boolean , default: false},
    billDate: {type: Date},
    bewo_ret_settlement_id: {type:String},

//更新功能

TransactionSettlement: function(req, res, next) {

    req.params.created_by = req.user._id;

     var filter = {};

     filter.body.merchant_reference_number = { $in: req.params.refnos };

//此处出现错误:无法设置未定义

的属性'merchant_reference_number'
            Merchant.findOne({ "body.merchant_reference_number": obj.refnos }).exec(function(err, doc) {

                console.log('err', err);
                console.log('doc', doc);
                doc.bewo_ret_settlement_id = obj.bewo_ret_settlement_id;
                doc.settlement_status = true;


                doc.save();
                res.send(doc);

            });

1 个答案:

答案 0 :(得分:0)

在为其指定属性之前,您需要先创建一个对象。在这种情况下,您可以这样做:

filter.body = {};
filter.body.merchant_reference_number = { $in: req.params.refnos };