I am saving a document to the db with the following code:
MongoClient.connect(mongoUrl, function(error, db) {
console.log('Mongo: error = \'' + error + '\' db = \'' + db + '\'');
insertDocument(db, parsedDocument);
db.close();
});
function insertDocument(db, document) {
db.collection('test').insertOne(document, function(error, result) {
console.log('Save document. error = \'' + error + '\' result = \'' + result + '\'');
});
}
However, if I leave the db.close()
statement in I get the an error in the error object. If I remove the line then the document is saved.
Received. ID: '04023828-9ef3-4f10-b1d1-108208c9e3b7'
Mongo: error = 'null' db = '[object Object]'
Save document. error = 'MongoError: server localhost:27017 sockets closed' result = 'undefined'
- Why is closing the db causing an error?
- Is it safe to not explicitly close the connection?