Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Await Receiving Warning while in try-catch Block

The first await below generates the warning 'await' has no effect on the type of this expression if I do not include the .catch line immediately found after (and currently commented out). However, all the code is included in a try-catch block. Why am I receiving this warning? Isn’t the MongoClient.connectan asynchronous call?

const db = null
async function dbConnect() {
  try {
    const client = await MongoClient.connect(url, {useUnifiedTopology: true})
    // .catch(err => { console.log(err) })
    if (!client) throw new Error("Database Not Available")
    db = await client.db(dbName)
  } catch (err) {
    console.log(err)
  }
}

dbConnect()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

.connect() returns a promise not a client. Try this code:

const db = null
async function dbConnect() {
  try {
    const client = new MongoClient(url)
    await client.connect({useUnifiedTopology: true})
    db = await client.db(dbName)
  } catch (err) {
    console.log(err)
  }
}

dbConnect()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading