MongoClient回调从未调用过

时间:2016-03-20 20:05:42

标签: node.js mongodb

我尝试使用nodejs本机驱动程序以多种方式连接到mongo db(3.0.6)。我刚刚开始编写我的应用程序,所以暂时我只有mongodb和mocha。

无论我尝试什么,我都无法回复/承诺执行。我知道当无法建立连接时会发生这种情况,但我也没有得到任何例外:

以下是我尝试过的一些选项:

    it('init with Promise', function () {
    var MongoClient = require('mongodb').MongoClient,
        test = require('assert');
    console.log("1\n");
    //, { server: { auto_reconnect: true } }
    MongoClient.connect('mongodb://localhost:27017/session').then(function (db) {
        // This line is never called
        console.log("2\n");
        // Get the collection
        var col = db.collection('insert_many_with_promise');
        col.insertMany([{a: 1}, {a: 2}]).then(function (r) {
            console.log("3\n");
            test.equal(2, r.insertedCount);
            // Finish up test
            db.close();
        });
    }).catch(function (error) {
        console.info(error);
    });
})


    it('init db', function () {
    var MongoClient = require('mongodb').MongoClient;
    var assert = require('assert');
    var ObjectId = require('mongodb').ObjectID;
    var url = 'mongodb://localhost:27017/test';

    var insertDocument = function (db, callback) {
        db.collection('restaurants').insertOne({
            "address": {
                "street": "2 Avenue",
                "zipcode": "10075",
                "building": "1480",
                "coord": [-73.9557413, 40.7720266]
            },
            "borough": "Manhattan",
            "cuisine": "Italian",
            "grades": [
                {
                    "date": new Date("2014-10-01T00:00:00Z"),
                    "grade": "A",
                    "score": 11
                },
                {
                    "date": new Date("2014-01-16T00:00:00Z"),
                    "grade": "B",
                    "score": 17
                }
            ],
            "name": "Vella",
            "restaurant_id": "41704620"
        }, function (err, result) {
            assert.equal(err, null);
            console.log("Inserted a document into the restaurants collection.");
            callback();
        });
    };

编辑: mongodb驱动版:" mongodb":" ^ 2.1.4"

1 个答案:

答案 0 :(得分:0)

您的测试在异步调用完成之前已经完成。添加 done 回调(请参阅Mocha中的异步代码测试)作为参数:

it('init with Promise', function (done) {
    var MongoClient = require('mongodb').MongoClient,
        test = require('assert');
    console.log("1\n");
    //, { server: { auto_reconnect: true } }
    MongoClient.connect('mongodb://localhost:27017/session').then(function (db) {
        // This line is never called
        console.log("2\n");
        // Get the collection
        var col = db.collection('insert_many_with_promise');
        col.insertMany([{a: 1}, {a: 2}]).then(function (r) {
            console.log("3\n");
            test.equal(2, r.insertedCount);
            // Finish up test
            db.close();
            done();
        });
    }).catch(function (error) {
        console.info(error);
        done(error);
    });
});