【问题标题】:node.js only passing filled out values to mongodbnode.js 仅将填写的值传递给 mongodb
【发布时间】:2017-08-01 21:41:19
【问题描述】:

我正在 node.js、mongodb 和 express 中处理一个必须处理事务的项目。 我可以硬编码要放入 mongodb 的变量,但如果没有填写所有字段(并非所有字段都是必需的),那么我只是将空数据放入数据库中。

此代码有效:

    var d = new Date();
    var n = d.toJSON();

    var date               = n;
    var address            = req.body.property_address;
    var client_name        = req.body.client_name;
    var sales_price        = req.body.sales_price;
    var commission_percent = req.body.commission_percent;
    var referral           = req.body.referral;
    var donation           = req.body.donation;
    var client_source      = req.body.client_source;
    var client_type        = req.body.client_type;
    var notes              = req.body.notes;

    Transaction.findOne({ name: { $regex: new RegExp(date, "i") } },
        function(err, doc){
            if(!err && !doc) {
                console.log("there is no error, and this does not already exist");
                var newTransaction = new Transaction();

                newTransaction.date               = date;
                newTransaction.address            = address;
                newTransaction.client_name        = client_name;
                newTransaction.sales_price        = sales_price;
                newTransaction.commission_percent = commission_percent;
                newTransaction.referral           = referral;
                newTransaction.donation           = donation;
                newTransaction.client_source      = client_source;
                newTransaction.client_type        = client_type;
                newTransaction.notes              = notes;

                newTransaction.save(function(err) {
                    if(!err){
                        console.log("successfully saved");
                        res.json(200, {message: newTransaction.date});
                    } else {
                        res.json(500, {message: "Could not create transaction. Error: " + err})
                    }
                });
            }
    });

但是将一个不存在的值放入 mongodb 有什么意义呢?这不是应该很棒的事情之一吗?我的想法是,它消除了我将来可能做的可能的查询。

是否有可能做更多这样的事情?

Transaction.findOne({ name: { $regex: new RegExp(date, 'i' ) } },
    function(err, doc){
        if(!err && !doc){
            var newTransaction = new Transaction();

            for(var key in req.body){
                newTransaction.key = req.body[key];
            }
        }
    }
});

更新

这是我最终使用的有效代码。

Transaction.findOne({ name: { $regex: new RegExp(date, "i") } },
    function(err, doc){
        if(!err && !doc){
            var newTransaction = new Transaction();
            for(var key in req.body){
                if(req.body[key] != ''){
                    newTransaction[key] = req.body[key];
                }
            }
        }
    }
});

【问题讨论】:

    标签: javascript node.js mongodb database


    【解决方案1】:

    这是非常好的行为。您仍然可以查询所有文档中不存在的字段,因为不存在的字段与不相等相同。

    这意味着仍然保留了文档的松散定义。

    【讨论】:

      【解决方案2】:

      对我来说看起来不错,但我可能不想只信任任何客户端数据作为密钥,这取决于您的部署方式。

      也许:

      ['date', 'address', 'client_name'/*,...*/].forEach(function(key)
      {
          /* ... */
          if (key in req.body) newTransaction[key] = req.body[key];
      });
      

      【讨论】:

      • 我并没有尝试将所有数据用作密钥。因为它是作为关联数组进来的,所以我试图使用键名作为 newTrancsaction 的名称。 'for(key in body)' 是我知道让它遍历所有数据的唯一方法。
      • @migpok35 这是同样的事情,但有一个允许的键列表。顺便说一句,我认为您的第二个代码中的意思是 newTransaction[key] 而不是 newTransaction.key。
      • 你一针见血,非常感谢。今晚我会睡得更舒服。
      猜你喜欢
      • 2012-01-13
      • 2018-12-29
      • 2016-06-01
      • 1970-01-01
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 2014-11-29
      • 2018-08-18
      相关资源
      最近更新 更多