简体   繁体   中英

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.connect an 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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM