NodeJS ASync call of MongoClient GetData routine

眉间皱痕 提交于 2021-01-29 17:58:11

问题


The code below is a mix of https://www.w3schools.com/nodejs/nodejs_mongodb_find.asp and https://stackoverflow.com/questions/49982058/how-to-call-an-async-function#:~:text=Putting%20the%20async%20keyword%20before,a%20promise%20to%20be%20resolved.

When you look at the console.log below the code, things seem to be running out of order. I thought by making the function async and using the .then I would avoid those issues.

I want the MongoDB data retrieval function separate from the app.get function. No data is being returned to the get request. I guess the app.get code is falling through and ending before the function returns the value. What do I need to correct?

async function getLanguageTranslationData(fromLanguage, toLanguage) {
    console.log("Started getLanguageTranslationData")
    const databaseUrl = "mongodb://localhost:27017"
    const databaseName = 'MyCompanyPOC'
    
    mongoClient.connect(databaseUrl, function(err, conn) {
        if (err) throw err; 
        const collectionName = "Phrases";
        var dbo = conn.db(databaseName)
        var query = 
                { $and: 
                       [ {masterLanguage: fromLanguage},
                         {localizedLanguage: toLanguage} 
                       ]
                }
        console.log("query=" + JSON.stringify(query)); 
        console.log("about to retrieve data"); 
        dbo.collection(collectionName).find(query).toArray( 
             function(err, result) {
                  if (err) throw err; 
                  console.log("Back from mongoDB.find()")
                  console.log(JSON.stringify(result))
                  return result 
                  conn.close()
           }) 
    })  
}


app.get("/api/gettranslations/:fromLanguage/:toLanguage", 
        async function(req, res) {
  console.log("Backend: /api/gettranslations method started: " + 
     " fromLanguage=" + req.params.fromLanguage + 
     " toLanguage=" + req.params.toLanguage)
  
  getLanguageTranslationData(
                             req.params.fromLanguage, 
                             req.params.toLanguage)
        .then((arrayResult) => { 
           console.log("got arrayResult back from getLanguageTranslationData")
           res.status(200).json(arrayResult)
           console.log("Backend: End of /api/gettranslations process")
         })
}) 

Node.JS Console output:

listening on port 3001
Backend: /api/gettranslations method started:  fromLanguage=en-US toLanguage=es-MX
Started getLanguageTranslationData
(node:44572) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
got arrayResult back from getLanguageTranslationData
Backend: End of /api/gettranslations process
query={"$and":[{"masterLanguage":"en-US"},{"localizedLanguage":"es-MX"}]}
about to retrieve data
Back from mongoDB.find()
[{"_id":"5f403f7e5036d7bdb0adcd09","masterLanguage":"en-US","masterPhrase":"Customers","localizedLanguage":"es-MX","localizedPhrase":"Clientes"},{ etc... 

回答1:


The thing is getLanguageTranslationData should return a promise so that you can use it as a promise in your handler, but in your case, call to getLanguageTranslationData will return undefined as all the code inside this function will execute asynchronously due to nodejs non-blocking nature.

So what you can do is return promise from your getLanguageTranslationData function like this.

function getLanguageTranslationData(fromLanguage, toLanguage) {
    const databaseUrl = "mongodb://localhost:27017"
    const databaseName = 'MyCompanyPOC'
    
    return new Promise((resolve, reject)=>{
        mongoClient.connect(databaseUrl, function(err, conn) {
            if (err) reject(err); 
            else{
                const collectionName = "Phrases";
                var dbo = conn.db(databaseName)
                var query = 
                        { $and: 
                               [ {masterLanguage: fromLanguage},
                                 {localizedLanguage: toLanguage} 
                               ]
                        }
                dbo.collection(collectionName).find(query).toArray( 
                     function(err, result) {
                          if (err) reject(err); 
                          else
                          resolve(result);    
                   }) 
            }
           
        }) 
    }) 
}

and then use await in your handler to use that promise returned

app.get("/api/gettranslations/:fromLanguage/:toLanguage", 
  async function(req, res) {
      try{
        let arrayResult = await getLanguageTranslationData(req.params.fromLanguage, req.params.toLanguage);
        res.status(200).json(arrayResult)
      }catch(err){
        // return error
      }
}) 

The above code will give you the gist of what you need to do, actual code may vary according to your needs.

You can refer async-await from here




回答2:


I got it working in this way, based on this example: Node.js, Mongo find and return data

@Namar's answer is probably correct too, but I was testing this the same time he posted. As the StackOverflow question/answer above notes, the up-to-date versions of MongoClient have support for promises. That post also shows how to put in a separate module, which is something I will probably do later this week.

function getLanguageTranslationData(fromLanguage, toLanguage) {
    console.log("Started getLanguageTranslationData")
    const databaseUrl = "mongodb://localhost:27017"
    const databaseName = 'ShedCompanyPOC'
    
    return mongoClient.connect(databaseUrl)
        .then(function(conn) {
            var collectionName = "UploadedDataeFromExcel";
            var dbo = conn.db(databaseName)
            var query = 
                    { $and: 
                           [ {masterLanguage: fromLanguage},
                             {localizedLanguage: toLanguage} 
                           ]
                    }
            console.log("query=" + JSON.stringify(query)); 
            console.log("about to retrieve data"); 
            var collection = dbo.collection(collectionName)
            return collection.find(query).toArray(); 
        }).then(function(result) {
              console.log("Back from mongoDB.find()")
              console.log(JSON.stringify(result))
              //conn.close()
              return result
        }); 
    }



app.get("/api/gettranslations/:fromLanguage/:toLanguage", 
        async function(req, res) {
  console.log("Backend: /api/gettranslations method started: " + 
     " fromLanguage=" + req.params.fromLanguage + 
     " toLanguage=" + req.params.toLanguage)
  
  getLanguageTranslationData(
                             req.params.fromLanguage, 
                             req.params.toLanguage)
        .then(function(arrayResult) {
           console.log("got arrayResult back from getLanguageTranslationData")
           res.status(200).json(arrayResult)
           console.log("Backend: End of /api/gettranslations process")
         }, function(err) {
             console.log("The promise was rejected", err, err.stack)
         })
}) 


来源:https://stackoverflow.com/questions/63605765/nodejs-async-call-of-mongoclient-getdata-routine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!