-
Type: Improvement
-
Resolution: Fixed
-
Priority: Minor - P4
-
Affects Version/s: 3.0.0, 2.2.33
-
Component/s: MongoDB 3.4
-
Environment:Any
-
Empty show more show less
At the moment. cursor.forEach is returning undefined.
So this code:
var mongo = require('mongodb') var pms = require('util').promisify ;(async function () { try { var db = await pms(mongo.MongoClient.connect)('mongodb://localhost/whatever', {}) var usersImages = db.collection('usersImages') var images = await usersImages.find({}) var i = await images.forEach(image => { console.log(image.image) }) console.log("RETURNED:", i) console.log('END OF CYCLE') } catch (e) { console.error('Error:', e) } })()
Doesn't work as expected, as RETURNED will be undefined and "END OF CYCLE" will display right away, before the resultset is returned.
This works:
var mongo = require('mongodb') var pms = require('util').promisify ;(async function () { try { var db = await pms(mongo.MongoClient.connect)('mongodb://localhost/whatever', {}) var usersImages = db.collection('usersImages') var images = await usersImages.find({}) images.forEachPms = pms(images.forEach) var i = await images.forEachPms(image => { console.log(image.image) }) console.log("RETURNED:", i) console.log('END OF CYCLE') } catch (e) { console.error('Error:', e) } })()
Here I basically promisify manually the forEach call.