Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9060263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:10:59+00:00 2026-06-16T15:10:59+00:00

I’m working on a project in node.js, mongodb, and express that has to deal

  • 0

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];
                }
            }
        }
    }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:11:01+00:00Added an answer on June 16, 2026 at 3:11 pm

    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];
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I know there's a lot of other questions out there that deal with this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.