繁体   English   中英

在 try-catch 块中等待接收警告

[英]Await Receiving Warning while in try-catch Block

如果我不包含之后立即找到的.catch行(并且当前已被注释掉),下面的第一个await会生成警告'await' has no effect on the type of this expression 但是,所有代码都包含在 try-catch 块中。 为什么我会收到此警告? MongoClient.connect不是异步调用吗?

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()返回 promise 而不是客户端。 试试这个代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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