These are chat archives for Automattic/mongoose

5th
Sep 2015
Erinç Fırtına
@EricMcRay
Sep 05 2015 10:16
@vkarpov15 Wow! nice feature! but too shame we are still use 3.8.x :D
Eric Martinez
@ericmartinezr
Sep 05 2015 21:15

Hey there! Is there anyway to "commit" after saving? Say I have this code

var data = {};
for(var i = 0; i < json.length; i++) {
    // Process data
    var m = new MyModel(data);
    m.save(function(err) {
        if(err) return console.error(err);
    });
    data = {};
}

// Something like commit or so here

MyModel.find({}, function(er, models)) {
    console.log(models);
});

Right after the for loop find returns nothing (by the way the save is working, I can see it in the shell). If I reload the page I can see what was saved

Just found the .post function, I think this is what I need
Eric Martinez
@ericmartinezr
Sep 05 2015 21:28
Nope, it is not. I don't need access the doc saved but to do something after all documents are saved. .post runs once everytime something is saved, this means that it will be triggered as many times as elements I have in my json. Any clue?
Eric Martinez
@ericmartinezr
Sep 05 2015 21:58
Got it, it is with MyModel.collection.insert(array, callback)