Node.js 将NodeJS与MongoDB驱动程序一起使用时,游标和集合始终为null?
我有下面的代码片段,它从不返回“user”集合中的数据。光标(项)始终为null/空Node.js 将NodeJS与MongoDB驱动程序一起使用时,游标和集合始终为null?,node.js,mongodb,Node.js,Mongodb,我有下面的代码片段,它从不返回“user”集合中的数据。光标(项)始终为null/空 var fs = require("fs"); var express = require("express"); var mongo = require("mongodb"); var config = JSON.parse( fs.readFileSync("config.json") ); var host = config.host; var port = config.port; var mong
var fs = require("fs");
var express = require("express");
var mongo = require("mongodb");
var config = JSON.parse( fs.readFileSync("config.json") );
var host = config.host;
var port = config.port;
var mongoHost = config.mongohost;
var mongoPort = mongo.Connection.DEFAULT_PORT;
var app = express();
var db = new mongo.Db("heelodb", new mongo.Server(host, port, {}));
app.use(app.router);
app.use(express.static(__dirname + "/public"));
app.get("/", function(request, response){
response.send("hello");
});
app.post("/api/checkEmail", express.bodyParser(), function(request, response) {
var email = request.body.email;
db.open(function(error){
console.log("We are connected to mongo! " + mongoHost + ":" + mongoPort);
var collection = db.collection("user");
collection.find().toArray(function(error, items){
console.log("collection name: " + collection.collectionName);
console.log("items: " + items);
});
});
});
app.listen(port, host);
当我直接查询mongodb数据库时,我总是返回结果。见下文
> db.user.find();
{ "_id" : ObjectId("52d6ef186a7a9c49af4d61c2"), "email" : "zaheer@heelo.co" }
{ "_id" : ObjectId("52d6ef1b6a7a9c49af4d61c3"), "email" : "zaheerbaloch@live.com" }
请帮忙 是否确实连接到了正确的数据库?例如mongodb://localhost:27017/nameOfYourDB@赫克托科里亚是的。代码中使用的数据库名称与在mongodb中创建的数据库名称相同。@HectorCorrea数据库名称正确,但主机和端口不正确。我使用的是主机和端口变量,而应该使用mongoHost和mongoPort变量!我现在觉得自己很愚蠢!谢谢你帮助我!