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

新手求助:MongooseError——Model.findOne()不再支持回调函数

解决MongooseError的方案
  • 检查并安装依赖
    代码中使用了encrypt插件但未引入,首先安装mongoose-encryption包:

    npm install mongoose-encryption
    

    然后在代码顶部添加引入语句:

    const encrypt = require('mongoose-encryption');
    
  • 完善数据库连接配置
    旧版Mongoose连接需补充useUnifiedTopology: true参数,否则会触发错误或警告:

    mongoose.connect("mongodb://localhost:27017/userDB", {
      useNewUrlParser: true,
      useUnifiedTopology: true
    });
    
  • 修正模型创建语法
    创建Mongoose模型不需要new关键字,修改为:

    const User = mongoose.model("User", userSchema);
    
  • 验证加密插件参数
    确认encryptedFields中的字段名与Schema定义完全匹配(当前代码中password匹配无误);另外建议将secret通过环境变量管理,避免硬编码敏感信息,当前参数格式本身无问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 20:22:01