Monday, 22 May 2017

How to use save() with a model instance in a for loop or each loop in NodeJS and mongodb?

I'm trying to save 6 model instances but for some reason it saving only the last one.

I tried it first with a simple for loop and then with async.each but it the same behavior for both.

Can anyone guide me what can I do? (I want it as simple as possible.)

Code:

MODEL_SERVICE.find({teamId:'B'}).then((Missions_result) => {
        let ModelInstance = new MODEL_SERVICE();
        async.each(Missions_result, function (mission, callback) {
            ModelInstance.missionId = mission.missionId;
            ModelInstance.teamId = body.email;
            ModelInstance.answer = mission.answer;
            ModelInstance.tryCount = 0;
            ModelInstance.save();       
        });
        res.json("User:"+body.email+",Password:"+body.password);
        }).catch((e) => {
            res.json(e);
        });

Thanks for your help!



via Ofear

No comments:

Post a Comment