Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
console.log gives value while return gives undefined

newbie here

let useDB = (action) =>{
    MongoClient.connect(url, (err, db)=>{
        if(err) throw err;
        let currentDB = db.db("mydb");
        let currentCol = currentDB.collection("items");

        let getData = ()=>{
            currentCol.findOne({}, (err, res)=>{
                if(err) throw err;
                return res <--- problem here
            })
        }
        if(action === "FIND") return getData()
    })
}

console.log(useDB("FIND"));

gives undefined while

let useDB = (action) =>{
    MongoClient.connect(url, (err, db)=>{
        if(err) throw err;
        let currentDB = db.db("mydb");
        let currentCol = currentDB.collection("items");

        let getData = ()=>{
            currentCol.findOne({}, (err, res)=>{
                if(err) throw err;
                console.log(res); <--- problem here
            })
        }
        if(action === "FIND") return getData()
    })
}

useDB("FIND")

works, this is a problem as i want the function to have the value of the data and not just log it to the console

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!