Home > front end >  Await Receiving Warning while in try-catch Block
Await Receiving Warning while in try-catch Block

Time:01-30

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()

CodePudding user response:

.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()
  •  Tags:  
  • Related