0

I've got a issue which i cannot figure out for the life of me, I have some code which uses a callback to manipulate a value returned by the database query and set another value which is outside of said callback, the value outside of the callback is not being set. Code is shown below:

module.exports.ReqForWelcomedCount = function ReqForWelcomedCount() {
    let WelcomedCount = 0;
    MongoClient.connect(uri, (err, client) => {
        if (err) { console.log("Error"); }
        var collection = client.db("agiobot").collection("data");
        let value = collection.find({}, { UsersWelcomed: { $exists:true}}).forEach(
            function (data, err) {
                WelcomedCount = data.UsersWelcomed;
                client.close();
            })
    })
    return WelcomedCount;
}

I'm a beginner in the world of more complex functions so, let me know if I've done anything silly.

0 Answers0