我是Node.js世界的新手。我已经将节点连接到我的MongoDB服务器。现在我想为MongoDB的CRUD操作编写模块,但是当我调用find
时,它会返回undefined
。
我该如何解决这个问题?
main.js
var MongoClient = require('mongodb').MongoClient;
var express = require('express');
var app = express();
var url = "mongodb://localhost:27017/movie";
var result;
var crud = require('./crud.js');
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("movie");
var test=crud.findAll(db,dbo,"test");
console.log(test);
});
var server = app.listen(8081, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Listening on "+host+":"+port);
})
crud.js
exports.findAll = function(db, dbo, collection){
dbo.collection(collection).find({}).toArray(function(err,result){
if (err) throw err;
db.close();
return result;
});
}