简体   繁体   中英

MongoClient.connect doesn't execute callback function

I'm relatively new to node.js. Trying to test connection to mongodb using mocha framework and mongodb driver.

Node.js version - 6.11.3

Mongodb driver version - 2.2.31

Mondodb version - 3.4.7

Here's my js file:

var should = require("should");
var expect = require('chai').expect;
var cfg = require('../config');
var uri = cfg.mongouri;
var MongoClient = require('mongodb').MongoClient, Logger = 
require('mongodb').Logger;

Logger.setLevel('debug');
describe("mongoconnection", function () {

describe("fetch data", function () {

    it("should fetch data from db", function (done) {
        MongoClient.connect(uri,function(err, db) {
                if (err) {
                    throw err;
                } else {
                    console.log("successfully connected to the database");
                }
                db.close();
            });
        done();
    });
});
});

However, this part of the code

function(err, db) {
            if (err) {
                throw err;
            } else {
                console.log("successfully connected to the database");
            }
            db.close();
        }

never gets executed and I can't establish connection, eg I don't get neither console log nor exception.

Debug information:

[DEBUG-Connection:9352] 1506430786041 creating connection 0 with options [{"host":HOST,"port":PORT,"size":5,"keepAlive":true,"keepAliveInitialDelay":300000,"noDelay":true,"connectionTimeout":30000,"socketTimeout":360000,"ssl":true,"ca":null,"crl":null,"cert":null,"rejectUnauthorized":false,"promoteLongs":true,"promoteValues":true,"promoteBuffers":false,"checkServerIdentity":true}] { type: 'debug', message: 'creating connection 0 with options [{"host":HOST,"port":PORT,"size":5,"keepAlive":true,"keepAliveInitialDelay":300000,"noDelay":true,"connectionTimeout":30000,"socketTimeout":360000,"ssl":true,"ca":null,"crl":null,"cert":null,"rejectUnauthorized":false,"promoteLongs":true,"promoteValues":true,"promoteBuffers":false,"checkServerIdentity":true}]', className: 'Connection', pid: 9352, date: 1506430786041 }

also already checked that connection string is correct and I can establish connection to it via another app (groovy script executed in SoapUI).

I am stuck at this point, can someone please help me with this, thanks in advance.

You are calling done() from Mocha outside of the async callback from the MongoClient.connect . So done() is called before it can even connect to the db.

Change your code to this:

it("should fetch data from db", function (done) {
    MongoClient.connect(uri,function(err, db) {
            if (err) {
                throw err;
            } else {
                console.log("successfully connected to the database");
            }
            db.close();
            done();
    });
});

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