我嘗試從節點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文件中的其他函數中對其進行操作。