简体   繁体   中英

Why do the Node.js mongodb driver methods not invoke callbacks

In the example below, I expect to see two lines from the console.log statements in the terminal but I only get one since the second callback is never fired. I'm using mongodb (2.4.8_1) and Node.js (0.10.22) from MacPorts with the official Node.js mongodb driver (1.3.20) from NPM (1.3.15). How can this happen?

var mongodb = require('mongodb');

mongodb.MongoClient.connect('mongodb://localhost:27017/test', function (err, db) {
    var query = { _id: new mongodb.ObjectID };

    console.log('connect!');

    db.collection('test').findOne(query, function (err, doc) {

        console.log('findOne!');

    });
});

Every time you run new mongodb.ObjectID it creates a new ID value - which you then instantly search for without inserting a document with. So how can it find a document, and hence show the second log line?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM