When using mongojs
's function db.collection.createIndex
in a for
loop, the result is unexpected as shown belo, where the same value of collectionName
is passed to createIndex()
every single time.
Why is this happening?
Code
var mongojs = require('mongojs');
var db = mongojs(config.get('Mongodb.url'));
for (collectionName of collectionNames) {
console.log(collectionName);
db[collectionName].createIndex({'userId': 1}, {'unique': true}, function() {
console.log('createIndexCB: ', collectionName);
})
}
Output
Fruits
Juices
Drinks
createIndexCB: Drinks
createIndexCB: Drinks
createIndexCB: Drinks