aserrin55 aserrin55 - 3 months ago 8
Node.js Question

nodejs trouble while updating array asynchronously

I have a problem with the asynchrony of nodejs. Hope you can help me.

exports.DeviceInfo = function(req, res, next) {
var nets = req.body.networks;

db.collection("device", function(err, deviceConf) {
var detected_beacons = [];

if (!err) {
for (var i = 0; i < nets.length; i++) {
deviceConf
.find({
"data.number": nets[i]
})
.toArray(function(errFind, saver) {
if (!errFind && saver) {
saver.forEach(function(disp) {
var detected = {};
detected.address = disp.address;
detected_beacons.push(detected);
console.log(detected_beacons);
});
}
});
}
console.log(detected_beacons);
res.json(detected_beacons);
} else {
console.error(err);
res.status(500);
res.json({
message: "Couldn't connect to database"
});
}
});
};


If I write some logs, we could see which is the problem. The detected_beacons is being returned before the for clause. I want to be returned after for in order to be full of data. I have read about callbacks and promises but I don't know how to use them in my code.

image

Answer

Try this code. you cannot do a for loop concerning callback functions.

exports.DeviceInfo= function(req,res,next){
var nets = req.body.networks;
db.collection("device", function(err, deviceConf){
    var detected_beacons = [];
    var netSize = nets.length;
    var beaconsLoop = function(i) {
        deviceConf.find({"data.number" : nets[i]}).toArray(function(errFind, saver){
                if (!errFind && saver){
                    var saverSize = saver.length;
                    saver.forEach(function(disp){
                        var detected = {};
                        detected.address = disp.address;
                        detected_beacons.push(detected);
                        saverSize--;
                        if (saverSize == 0) {
                            if (i != netSize - 1) {
                                i++;
                                beaconsLoop(i);
                            } else {
                                res.json(detected_beacons);
                            }
                        }
                    });
                }
            });
    };

    if (!err){
        if (netSize > 0) beaconsLoop(0);
    } else {
        console.error(err);
        res.status(500);
        res.json({message:"Couldn't connect to database"});
    }
});

};