我尝试从节点JS文件中的mongo DB数据库中恢复对象,但它不起作用。在节点JS上恢复使用MongoDB驱动程序请求的对象
在一个名为db.js,我做了下面的代码:
var MongoClient = require('mongodb').MongoClient;
module.exports = {
FindinColADSL: function() {
return MongoClient.connect("mongodb://localhost/sdb").then(function(db) {
var collection = db.collection('scollection');
return collection.find({"type" : "ADSL"}).toArray();
}).then(function(items) {
return items;
});
}
};
而且,我尝试使用它在文件server.js:
var db = require(__dirname+'/model/db.js');
var collection = db.FindinColADSL().then(function(items) {
return items;
}, function(err) {
console.error('The promise was rejected', err, err.stack);
});
console.log(collection);
在结果我有“承诺{}”。为什么?
我只想从数据库中获取一个对象,以便在位于server.js文件中的其他函数中对其进行操作。