我有以下代码片段,它永远不会返回“user”集合中的数据。光标(项目)始终为空/空。
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" }
请帮忙!