简体   繁体   中英

Return value from a mongodb query from nodejs

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 }

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.

eg 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);
            });
        });
    });
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM