Mongoose: TypeError: Cannot read property 'hasListCollectionsCommand' of null

Created on 6 Mar 2016  路  1Comment  路  Source: Automattic/mongoose

I'm using mongoose v4.4.6 & node v5.4.1

var mongoose = require('mongoose');
var conn = mongoose.createConnection("mongodb://localhost/xxxx");

When I call

conn.db.listCollections();

or

conn.db.collections();

There is an error.

TypeError: Cannot read property 'hasListCollectionsCommand' of null
    at Db.listCollections (~/Project/node_modules/mongodb/lib/db.js:619:38)
    at Object.<anonymous> (~/Project/test.js:6:9)
    at Module._compile (module.js:397:26)
    at Object.Module._extensions..js (module.js:404:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:429:10)
    at startup (node.js:139:18)
    at node.js:999:3

Most helpful comment

Problem solved.

conn.on('open', function () {
    conn.db.listCollections().toArray(function (err, names) {
        console.log(err, names);
        conn.close();
    });
});

>All comments

Problem solved.

conn.on('open', function () {
    conn.db.listCollections().toArray(function (err, names) {
        console.log(err, names);
        conn.close();
    });
});
Was this page helpful?
0 / 5 - 0 ratings