Node.js nodejs中的MongoDb驱动程序不调用开放函数回调
在下面的代码中,传递给open函数的函数永远不会运行,然后下面代码中的istruction console.log('open!')永远不会运行:Node.js nodejs中的MongoDb驱动程序不调用开放函数回调,node.js,mongodb,Node.js,Mongodb,在下面的代码中,传递给open函数的函数永远不会运行,然后下面代码中的istruction console.log('open!')永远不会运行: var mongo = require("mongodb"); var Db = mongo.Db; var connection = mongo.Connection; var Server = mongo.Server; var client = new Db('test', new Server("localhost", 27017, {})
var mongo = require("mongodb");
var Db = mongo.Db;
var connection = mongo.Connection;
var Server = mongo.Server;
var client = new Db('test', new Server("localhost", 27017, {}));
var test = function (err, collection) {
collection.insert({a:2}, function(err, docs) {
collection.count(function(err, count) {
test.assertEquals(1, count);
});
// Locate all the entries using find
collection.find().toArray(function(err, results) {
test.assertEquals(1, results.length);
test.assertTrue(results[0].a === 2);
// Let's close the db
client.close();
});
});
};
client.open(function(err, p_client) {
console.log('open!');
client.collection('test_insert', test);
});
从日志中,我看到连接已被接受:
Sun March 11 16:52:01 [initandlisten] accepted connection from 127.0.0.1:61875 # 1
Mongodb是InteractiveShell的优秀作品
有人能告诉我一些建议吗
谢谢大家! 复制/粘贴此…它应该可以正常工作:
var client = new Db('test', new Server("localhost", 27017, {}), {});
client.open(function(err, client) {
console.log('open!');
}
另外,不要忘记在打开连接后进行身份验证
var client = new Db('test', new Server("localhost", 27017, {}), {});
client.open(function(err, client) {
console.log('open!');
client.authenticate('admin', 'admin', function(err, result) {
// Authenticated
});
}
谢谢你的回答。我对这个代码也有同样的问题。是的,它记录为“asdf”。相反,只有当mondodb关闭时,open callback才会运行:(您使用的是什么版本的节点?您使用的是什么版本的节点驱动程序?进一步回答您的问题,我已升级到最新版本的节点,我重新安装了驱动程序,现在一切正常。谢谢!