mongodb - Create promises from 2D array in node.js -


to import several unrelated csv files mongodb, parse files client side , send "import maps" node. there passed function upsert them mongoose/mongodb. function works , similar following (less promises parts).

import.prototype.saveimport = function (importarray) {     var writes = []; //promises     (var = 0; < importarray.length; i++) { //loop models         var model = mongoose.model(importarray[i].model);         (var j = 0; j < importarray[i].objects.length; j++) { //loop objects             writes.push(function () {                 return new promise(function (resolve, reject) {                     var obj = importarray[i].objects[j];                      var searchobj = {};                     (var k = 0; k < importarray[i].importindex.length; k++) {                         var index = importarray[i].importindex[k];                         searchobj[index] = obj[index];                     }                      model.update(searchobj, {                         $set: obj                     }, {                         upsert: true                     }, function (err, raw) {                         if (err)                             console.log(err);                         resolve();                     });                 });             });         }     }     promise.all(writes)         .then(function () {             console.log('all done');         })         .catch(console.error); }; 

this function not work several reasons, illustrate trying do. need way wait mongodb writes complete can send reply client. @ loss on how achieve it.

promises seemed simplest way this. how add promises inside second loop have access outer variables? open other options though preferably without other libraries.

one way can create new promise promise.all every row , push new array , call promise.all.

for (var = 0; < write.length; i++) {     promisearr.push(new promise(function(resolve, reject) {         promise.all(writes[i])         .then(resolve)         .catch(reject);     })); }  promise.all(promisearr) .then(function () {     console.log('all done'); }) .catch(console.error); 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -