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

Mongoose弃用警告与MongoParseError:连接MongoDB Atlas失败求助

MongoDB Atlas连接问题修复方案

一、解决MongoParseError: mongodb+srv URI cannot have port number错误

虽然你提供的连接字符串本身未包含端口号,但出现该错误通常有以下几种排查方向:

  • 核对.env文件内容:确保连接字符串没有被误添加端口后缀(如:27017),你当前的字符串格式是正确的:
    DATABASE_URI=mongodb+srv://databasename:hjfksf432FDS34@cluster0.6mxghis.mongodb.net/?retryWrites=true&w=majority
    
  • 检查代码逻辑:确认在调用数据库连接的代码中,没有手动给URI拼接端口参数,比如避免${process.env.DATABASE_URI}:27017这类操作。
  • 重置依赖包:若因依赖版本冲突导致解析异常,执行以下命令清理并重新安装依赖:
    rm -rf node_modules package-lock.json
    npm install
    

二、消除Mongoose strictQuery弃用警告

在初始化Mongoose连接前,添加以下代码即可处理警告:

// 放在mongoose.connect()调用之前
mongoose.set('strictQuery', false); // 适配Mongoose 7的默认行为
// 若想保留当前严格查询模式,也可设置为true直接抑制警告
// mongoose.set('strictQuery', true);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 11:35:19