You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Model.findOne()不再接受回调函数的代码报错?

修复方案

这个错误是因为新版Mongoose不再支持findOne()方法传入回调函数,需要改用Promise链式调用或者async/await语法来处理异步操作。

方法1:使用Promise链式调用

List.findOne({ name: tagname })
  .then(fidlist => {
    if (!fidlist) {
      console.log("doesnt exist");
    } else {
      console.log("exist");
    }
  })
  .catch(err => {
    console.error(err);
  });

方法2:使用async/await(推荐)

如果你的代码在异步函数中,可以用更简洁的async/await:

async function checkListExists() {
  try {
    const fidlist = await List.findOne({ name: tagname });
    if (!fidlist) {
      console.log("doesnt exist");
    } else {
      console.log("exist");
    }
  } catch (err) {
    console.error(err);
  }
}

// 调用函数
checkListExists();

注意:使用async/await时必须包裹在async函数内,同时用try/catch捕获错误,替代原来的回调错误处理逻辑。

内容的提问来源于stack exchange,提问作者Dhiraj Jadhav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 00:12:42