0

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 }
5
  • Interesting. I wonder if it's a this related issue. Commented Apr 1, 2019 at 17:21
  • 1
    getTypes() doesn't return anything Commented Apr 1, 2019 at 17:27
  • @Andreas Do you have any idea how I could fix that? Commented Apr 1, 2019 at 17:37
  • You try to mix synchronous and async functions Commented Apr 1, 2019 at 17:52
  • use async or call backs Commented Apr 1, 2019 at 18:05

2 Answers 2

3

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);
            });
        });
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, very helpful.
1

getTypes doesn't return anything. You've gotta pass it up If you're gonna use async/await try something like

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

These might be helpful: How can I use asyn-await with mongoclient and how-to-use-mongodb-with-promises-in-node-js

Also, you should probably close the connection with db.close() somewhere

1 Comment

OK, thanks. I will check these links. I tried your suggestion and it did not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.