简体   繁体   中英

node.js only passing filled out values to mongodb

I'm working on a project in node.js, mongodb, and express that has to deal with transactions. I can hard code the variables to be put into mongodb, but if not all the fields are being filled out (not all are required) then I'm just putting empty data into the database.

this code works:

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

But what's the point of putting in a value that doesn't exist into mongodb? Isn't that one of the things that is supposed to be awesome about it? And my thinking is, it takes away possible queries that I might be able to do in the future.

Would it be possible to do something more like this?

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

update

This is the code I ended up using that worked.

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

This is perfectly fine behaviour. You can still query a field that doesn't exist in all documents, as a non-existent field is the same as not equal.

This means the loose definition of the documents is still retained.

Looks fine to me, but I would probably not want to trust just ANY client data to serve as key, depending on how you deploy.

Perhaps:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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