Insert (using safe:true) not inserting anything to MongoDB in nodeJS, despite saying it does

Problem

I have a relatively extensive webapp that is all built in NodeJS and MongoDB using the mongodb-node-driver. Right now I've come across the most bizarre of issues.

I have this function:

function newLoc(req, res){
    var newloc = req.body;
    console.log(JSON.stringify(newloc));
    locs.insert(newloc,{safe:true},function(err,doc){
        if(err) returnError(res,'aww shit locinsertion error!\n\n'+err.message);
        else{
            console.log(doc[0]);
            returnSuccess(res,"The ID of the location you just added is: "+doc[0]['_id']);
        }
    });
}

Everything is set up properly (in terms of connection opening) and returnSuccess works just fine. req.body contains the data coming from a form that is sent via POST. Both of the console.log print the same thing, except the second one has an "_id" added and all of the keys don't have quotations around them.

However, NOTHING makes it to the database. No errors, the document is returned as if it was inserted, yet nothing is added. This is especially bizarre because this is not a problem ANYWHERE else in my code (and i do lots of inserts).

To add to the confusion, if I change newloc in the insert to be {foo:bar}, it does insert. Does anybody have any idea as to what might be going on?

Edit in response to christkv's request, here are the objects printed:

{"music":"art","array":[],"name":"sdf","info":"","date":"asdf","_id":"fSURBOvCU"}

{ muic: 'art',
  array: [],
  name: 'sdf',
  info: '',
  date: 'asdf',
  _id: 'fSURBOvCU' }

and nothing is being printed in the "mongod" process output, which i assume is where any errors would be logged as well.

also, when console.log(JSON.stringify(newloc)) has the JSON.stringify removed, the outputs are identical.

Problem courtesy of: thisissami

Solution

Ok I think I've figured it out. My collection creation code originally looked like this:

  db.createCollection('place', function(err, pplace){
  if(err){
    console.log('db.createCollection error!');
    console.log(err);
    return;
  }
        db.createCollection('tips', function(err, tipps){
            if(err){
                console.log('tips didnt get made: \n'+err);
            }
            else{
                                    place = pplace;
                tips = tipps;
                console.log(' place tips created');
            }
        }); 
 });

I don't create place or tips anywhere else since javascript automatically makes something a global if you don't give it a "var".

Now, I moved place = pplace to be right under the if statement after it's creation, and i also added a var place, tips; up above. This seems to have fixed the issue, and now everything I throw at my code seems to be inserted properly into my collection.

I'm still completely confused as to how this would change anything though, seeing as how I was getting the usual response for a SUCCESSFUL insert, not a failed one... Any thoughts?

Solution courtesy of: thisissami

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.