javascript - Creating unique Mongoose objects from CSV -


let's have csv containing information needs parsed different mongoose objects. need either find or create "user" based on information in csv file. if run this, however, create new user each time, since not wait until database save has finished execute rest of csv.

fs.readfile(file, function(err, data) {   if (err) throw err;   parse(data, function(err, output) {     if (err) throw err;      user.findone({name: output[0] }, function(err, user) {       if (err) throw err;        if (!user) {         var user = new user({ name: output[0] });         user.save(function(err, user) {           anotherfunctionwithrestofdata(user, output);         });       } else {         anotherfunctionwithrestofdata(output);       }     });   }) }) 

how can control duplicate data not being saved?

thanks.

you can use async's eachseries method.

i assume output array of users. async's eachseries iterate on array, process item and, once callback method called, go next item in array :

fs.readfile(file, function(err, data) {     if (err) throw err;     parse(data, function(err, output) {         if (err) throw err;          async.eachseries(output, function(user, callback){             // stuff user             // when it's done, go next 1 calling callback             callback(err);         },function(err){             // handle errors         });      }) }) 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -