Ad

Return Value From A Mongodb Query From Nodejs

- 1 answer

EDIT

OK I read here."You can't usefully return with asynchronous functions. You'll have to work with the result within the callback. This is due to the nature of asynchronous programming: "exit immediately, setting up a callback function to be called sometime in the future. And, at least with the current standard of ECMAScript 5, you can't get around this. As JavaScript is single-threaded, any attempt to wait for the callback will only lock up the single thread, keeping the callback and the return user forever pending in the event queue."

Is this still the case today?

ORIGINAL QUESTION

I have a problem accessing my variable outside the function in my node.js application.

const url = "mongodb://localhost:27017/";
getAllSampleTypes();

// I would like to have the variable "requested" accessible here

function getAllSampleTypes() {
    MongoClient.connect(url, function (err, db) {
        var dbo = db.db("myDb");
        dbo.collection("data").distinct("sample_type", {}, (function (err, requested) {
// variable "requested" is accessible here

            })
        );

    });
}

I tried with async/await but I still have the same problem.

function getTypes() {
    MongoClient.connect(url, async function (err, db) {
        let dbo = db.db("myDb");
        return await dbo.collection("data").distinct("sample_type", {});

    });
}
console.log(getTypes()); //Promise { undefined }
Ad

Answer

I don't think you are going to be able to achieve what you are looking for. Async await only works once you are in scope of an async function. Your top level calls are not inside an async function so you are forced to handle the returned Promise or callback.

e.g. getAllSampleTypes().then(function(response){});

Here are a couple of samples that are similar to what you want, but either way, the top level call into an async function will have to handle the response as a Promise.

const url = "mongodb://localhost:27017/";

getAllSampleTypes().then(function(sample_types){
    // Do something here.
});


async function getAllSampleTypes() {
    var db = await mongo.connect(url);
    var dbo = db.db("myDb");
    return await dbo.collection("data").distinct("sample_type", {});
}

It's important to understand that async await really isn't anything magical, behind the scenes it's translated to Promises really. That's why your top level call into an async function can handle the response with a .then(). It's just really much cleaner to read. The code above would roughly get translated and executed as:

const url = "mongodb://localhost:27017/";

getAllSampleTypes().then(function(sample_types){
    // Do something here.
});

function getAllSampleTypes() {
    return new Promise(function(resolve, reject){ 
        mongo.connect(url).then(function(db){
            var dbo = db.db("myDb");
            dbo.collection("data").distinct("sample_type", {}).then(function(results) {
                resolve(results);
            });
        });
    });
}
Ad
source: stackoverflow.com
Ad