Home > Software engineering >  async await data return from backend node.js
async await data return from backend node.js

Time:08-31

I have this function:

module.exports = async function(username, password, mongo_uri, cluster, collection) {
    
    let client = new MongoClient(mongo_uri)
    let database = client.db(cluster)
    let collection_name = database.collection(collection)

    
    if (!username || !password || !username && !password) {
        return false
    } else {
        //query mongo
       MongoClient.connect(mongo_uri, async function(err, db) {
            if (err) {
                console.log(err);
            }
            collection_name.find({'email': username}).toArray(async function(e,doc){
                if(doc.length === 0) {
                    db.close();
                    return false
                } else {
                    db.close();
                    return true
                }
            });
        })
    }
 } 

and I have this in the front-end:

 let results = await mongo_login_api(username, password, mongo_uri, cluster, collection)
console.log("results")
console.log(results)

when I run this function, it doesn't wait for the backend to return anything. How can I fix this?

CodePudding user response:

My suggestion is:

You put all body of your funcion in Promise.

For example:

module.exports = function(username, password, mongo_uri, cluster, collection) {
    return new Promise((resolve, reject) => {
       let client = new MongoClient(mongo_uri)
       let database = client.db(cluster)
       let collection_name = database.collection(collection)

    
       if (!username || !password || !username && !password) {
          resolve(false)
       } else {
        
        
        //query mongo
       MongoClient.connect(mongo_uri, async function(err, db) {
            if (err) {
                console.log(err);
            }
            collection_name.find({'email': username}).toArray(function(e,doc){
                if(doc.length === 0) {
                    db.close();
                    resolve(false)
                } else {
                    db.close();
                    resolve(true) 
                }
            });
        })
      }
     }
    }
 }

CodePudding user response:

You need to promise the MongoClient.connect callback to await it.

module.exports = async function(username, password, mongo_uri, cluster, collection) {
    let client = new MongoClient(mongo_uri)
    let database = client.db(cluster)
    let collection_name = database.collection(collection)

    if (!username || !password || !username && !password) {
        return false
    } else {
        const result = await new Promise((resolve, reject) => {
            //query mongo
            MongoClient.connect(mongo_uri, function(err, db) {
                if (err) {
                    console.log(err);
                }
                collection_name.find({
                    'email': username
                }).toArray(function(e, doc) {
                    if (doc.length === 0) {
                        db.close();
                        resolve(false)
                    } else {
                        db.close();
                        resolve(true)
                    }
                });
            })
        })
        return result
    }
}
  • Related