I have a weird issue where I have a test to see prove that you cannot create a new user if a user with the same name/email exists, however it always seems to fail. However when I look at the first step which adds a user to the database with the details expected, it inserts and calls back with the document:
[ { Username: 'AccountUser',
Email: 'some@email.com',
CreatedDate: Mon Sep 22 2014 12:52:48 GMT+0100 (GMT Summer Time),
_id: 54200d90d0a34ffc1565df13 } ]
So if I do a console.log of the documents returned from insert thats what I get which is ok, and the _id has been set by mongo which is correct, so I know the call succeeded, and there was no errors.
However if I then go and view the database (MongoVUE) that collection is empty, so I am a bit baffled as to why this is happening.
I am using pooled mongodb connections (i.e setting up during app setup then using app.set("database_connection", database);. So I am a bit baffled as to why this is happening?
Here is some example code, which is part of larger code but basically is contained within promises:
// Point of connection creation
var mongoClient = mongodb.MongoClient;
var connectionCallback = function (err, database) {
if (err) { reject(err); }
resolve(database);
};
try
{ mongoClient.connect(connectionString, connectionCallback); }
catch(exception)
{ reject(exception); }
// Point of usage
var usersCollection = connection.collection("users");
usersCollection.insert(userDocument, function(err, docs) {
if(err) {
reject(err);
return;
}
console.log(docs);
resolve(docs[0]);
});
So I create the connection and pass back the database for use elsewhere, then at the point of inserting I get the connection, get the collection and then insert into it, and log the documents added, which is what is in the above log output.
Also finally running:
Not the answer I was hoping for, but I restarted the computer (rare occurrence) and the issue no longer occurs. I have no idea what was causing it and I am still slightly worried incase it happens again, but for now I am up and running again.