Question

I'm having trouble retrieving data from a mongodb collection which I believe has been inserted correctly.

So here is my example code...

var db = require('./database');

module.exports = function (app) {
    app.get('/db', function (req, res) {
        db.collection('myCollection', function (err, myCollection) {
            if (err) {
                return console.error(err);
            }

            var docrow = {
                // no id specified, we'll let mongodb handle that
                name: 'Mark',
                date: '2013/09/11',
                description: 'Some text here'
            };

            console.log('I GET HERE OK');

            myCollection.insert(docrow, { safe: true }, function (err, insertedDocument) {
                console.log('BUT I DONT GET HERE?');

                if (err && err.name === 'MongoError' && err.code === 11000) {
                    return console.log('This document already exists');
                } else if (err) {
                    return console.log('Something bad happened');
                }

                myCollection.find({ name: 'Mark' }, function (err, docs) {
                    docs.each(function (err, doc) {
                        console.log(doc);
                    });
                });
            });


            res.end('OK we made it');
        });
    });
};

...and the database.js file is...

var Db         = require('mongodb').Db,
    Connection = require('mongodb').Connection,
    Server     = require('mongodb').Server;

var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;

/*
    w:1 tells mongo to wait until at least one confirmed write has succeeded before calling any callbacks
 */
var flags = { w: 1 };
var server = new Server(host, port, { auto_reconnect: true, poolSize: 20 });
var db = new Db('TestDBName', server, flags);

module.exports = db;

It looks like I'm able to create a Collection (myCollection) without error, and calling insert on the collection doesn't error either, but also doesn't appear to get any where near inside the callback function for it to trigger either an error or handle a success?

What am I doing wrong here?

Thanks for any help you can give me.

Was it helpful?

Solution

When you connect to mongodb it is asynchronous method, so it will return client handler in callback, and this client handler have to be used onwards instead of handle of that Db object. So change this:

var db = new Db('TestDBName', server, flags);

To this:

new Db('TestDBName', server, flags).open(function(err, client) {
  if(err) throw err;

  // client - is the guy you are looking for instead of `db` you had
});

As well change:

myCollection.find({ name: 'Mark' }, function (err, docs) {

To:

myCollection.find({ name: 'Mark' }).toArray(function (err, docs) {

It is the only exception with mongo-native where you have to use .toArray instead of direct callback.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top