我該如何打印集合中的所有項目。我正在使用此代碼,並且可以在集合中少於100個項目的情況下正常工作。如何從承諾中打印100多份來自mongoDB集合的文檔
當我有更多的只是印刷:
ITEMS: undefined
1
ITEMS: undefined
2
.....
ITEMS: undefined
99
ITEMS: undefined
100
ITEMS: undefined
C:\Users\rmuntean\Documents\Automatizare\NodeJS\node_modules\mongodb\lib\utils.js:98 process.nextTick(function() { throw err; });
TypeError: callback is not a function
我也試過指定者,是同樣的問題。
沒有承諾的代碼工作正常,我可以打印所有項目。
var bluebird = require('bluebird');
var MongoClient = require('mongodb').MongoClient;
var MongoCollection = require('mongodb').Collection;
bluebird.promisifyAll(require('mongodb'));
const connection = "mongodb://localhost:27017/test";
var cc = 0;
var theDb
var theCollection
MongoClient.connectAsync(connection)
.then(function(db) {
theDb = db;
return theDb.collectionAsync("test_array");
})
.then(function(collection) {
theCollection = collection;
return theCollection.findAsync({});
})
.then(function(cursor) {
cursor.forEach((err, items) => {
console.log("ITEMS:", items);
cc++
console.log(cc);
});
})
.finally(() => {
theDb.close()
})
.catch((err) => {
console.log(err);
err(500);
});
我使用:
"mongodb": "^2.2.12",
"bluebird": "^3.4.6",
我在做什麼錯?
'cursor.forEach((ERR,項目)'你不在這裏返回任何東西,所以你最終獲取調用立竿見影。你可以包裝在foreach一個無極裏面,我beleive當所有項目完成項目' '(將會是錯誤的。) – Keith