经过15年的VB6和MySql,我是node和mongo的新手.我确定这不是我的最终程序将使用的,但我需要基本了解如何在另一个模块中调用函数并获得结果.
我希望模块具有打开数据库的功能,在集合中查找并返回结果.我可能想在该模块中为其他集合添加更多功能.现在我需要它尽可能简单,我可以稍后添加错误处理程序等.我花了好几天尝试了不同的方法,module.exports = {...围绕着这个函数而不是它,.send,返回所有方法,没有运气.我知道它是异步的,因此程序可能在数据存在之前通过了显示点.
这是我用Mongo运行db1数据库和col1集合的尝试.
Db1.js
var MongoClient = require('mongodb').MongoClient;
module.exports = {
FindinCol1 : function funk1(req, res) {
MongoClient.connect("mongodb://localhost:27017/db1", function (err,db) {
if (err) {
return console.dir(err);
}
var collection = db.collection('col1');
collection.find().toArray(function (err, items) {
console.log(items);
// res.send(items);
}
);
});
}
};
app.js
a=require('./db1');
b=a.FindinCol1();
console.log(b);
Run Code Online (Sandbox Code Playgroud)
CONSOLE.LOG(项目)工作的时候,"FindinCol1"电话,但不能CONSOLE.LOG(B)(返回"不确定"),所以我没有得到回报,不然我的时候粘贴它的回报.我已经阅读了数十篇帖子并观看了数十个视频,但我仍然坚持这一点.任何帮助将不胜感激.
eco*_*utu 25
正如另一个答案所提到的,这段代码是异步的,你不能简单地在回调链(嵌套函数)中返回你想要的值.您需要公开一些接口,让您在获得所需值后发出呼叫代码信号(因此,回调它们或回调).
在另一个答案中提供了一个回调示例,但是有一个替代选项绝对值得探索:promises.
您可以使用所需结果调用回调函数,而不是使用所需结果调用的回调函数,该模块将返回一个可以进入两个状 调用代码等待承诺进入这两种状态之一,当它执行时调用适当的函数.模块通过resolveing或rejecting 触发状态更改.无论如何,这是一个使用promises的例子:
Db1.js:
// db1.js
var MongoClient = require('mongodb').MongoClient;
/*
node.js has native support for promises in recent versions.
If you are using an older version there are several libraries available:
bluebird, rsvp, Q. I'll use rsvp here as I'm familiar with it.
*/
var Promise = require('rsvp').Promise;
module.exports = {
FindinCol1: function() {
return new Promise(function(resolve, reject) {
MongoClient.connect('mongodb://localhost:27017/db1', function(err, db) {
if (err) {
reject(err);
} else {
resolve(db);
}
}
}).then(function(db) {
return new Promise(function(resolve, reject) {
var collection = db.collection('col1');
collection.find().toArray(function(err, items) {
if (err) {
reject(err);
} else {
console.log(items);
resolve(items);
}
});
});
});
}
};
// app.js
var db = require('./db1');
db.FindinCol1().then(function(items) {
console.info('The promise was fulfilled with items!', items);
}, function(err) {
console.error('The promise was rejected', err, err.stack);
});Run Code Online (Sandbox Code Playgroud)
现在,更多最新版本的node.js mongodb驱动程序对promises有本机支持,你不需要做任何工作来在上面的promises中包装回调.如果您使用的是最新的驱动程序,这是一个更好的示例:
// db1.js
var MongoClient = require('mongodb').MongoClient;
module.exports = {
FindinCol1: function() {
return MongoClient.connect('mongodb://localhost:27017/db1').then(function(db) {
var collection = db.collection('col1');
return collection.find().toArray();
}).then(function(items) {
console.log(items);
return items;
});
}
};
// app.js
var db = require('./db1');
db.FindinCol1().then(function(items) {
console.info('The promise was fulfilled with items!', items);
}, function(err) {
console.error('The promise was rejected', err, err.stack);
});Run Code Online (Sandbox Code Playgroud)
Promise为异步控制流提供了一种很好的方法,我强烈建议您花一些时间熟悉它们.
是的,这是一个异步代码,根据您放置的位置,return您将获得MongoClient对象或什么都没有。
您应该使用回调参数:
module.exports = {
FindinCol1 : function funk1(callback) {
MongoClient.connect("mongodb://localhost:27017/db1", function (err,db) {
if (err) {
return console.dir(err);
}
var collection = db.collection('col1');
collection.find().toArray(function (err, items) {
console.log(items);
return callback(items);
});
});
}
};
Run Code Online (Sandbox Code Playgroud)
将回调函数传递给FindinCol1:
a.FindinCol1(function(items) {
console.log(items);
});
Run Code Online (Sandbox Code Playgroud)
我建议你查看这篇文章:https : //docs.nodejitsu.com/articles/getting-started/control-flow/what-are-callbacks