我需要使用db数据数组获得变量 x ,但我无法做到。 当我在 findReviews 函数中调试.log( x )时,一切正常,但是当我从 findReviews 和console.log()返回x变量时x )外部函数我收到错误: ReferenceError:x未定义。我如何将这个变量“导出”到外面并在其他地方使用?
var url = 'mongodb://localhost:27017/instagramdb';
//Get From DB Reviews
function findReviews(db, callback) {
// Get the documents collection
var collection = db.collection('randComments');
// Find some documents
collection.find({}).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
callback(docs);
});
}
// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to server");
findReviews(db, function (docs) {
var x = docs
return x
});
console.log(x);
db.close();
});