Cry*_*ler 12 uri connect mongoose
mongoose
.connect(db,{ useNewUrlParser: true})
.then(() => console.log("MongoDB conected ..."))
.catch(err => console.log(err));
Run Code Online (Sandbox Code Playgroud)
上面提到的是我的代码,其中“db”是由 mLab 提供的我的数据库 URI
当我编写运行快速代码时,它会捕获错误并给出特定的错误消息:
URI 格式错误,无法解析
{ MongoError: URI malformed, cannot be parsed
at parseConnectionString (/Users/dibs/Desktop/mern-ex/node_modules/mongodb-core/lib/uri_parser.js:207:21)
at connect (/Users/dibs/Desktop/mern-ex/node_modules/mongodb/lib/operations/mongo_client_ops.js:179:3)
at connectOp (/Users/dibs/Desktop/mern-ex/node_modules/mongodb/lib/operations/mongo_client_ops.js:283:3)
at executeOperation (/Users/dibs/Desktop/mern-ex/node_modules/mongodb/lib/utils.js:420:24)
at MongoClient.connect (/Users/dibs/Desktop/mern-ex/node_modules/mongodb/lib/mongo_client.js:168:10)
at Promise (/Users/dibs/Desktop/mern-ex/node_modules/mongoose/lib/connection.js:487:12)
at Promise (<anonymous>)
at NativeConnection.Connection.openUri (/Users/dibs/Desktop/mern-ex/node_modules/mongoose/lib/connection.js:484:19)
at Mongoose.connect (/Users/dibs/Desktop/mern-ex/node_modules/mongoose/lib/index.js:229:15)
at Object.<anonymous> (/Users/dibs/Desktop/mern-ex/server.js:20:6)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
name: 'MongoParseError',
message: 'URI malformed, cannot be parsed',
[Symbol(mongoErrorContextSymbol)]: {} }
Run Code Online (Sandbox Code Playgroud)
首先检查变量/env变量是否可以登录到控制台
console.log(db);
Run Code Online (Sandbox Code Playgroud)
如果您是从其他文件而不是入口点执行您的应用程序,则将其添加到该其他文件中
require('dotenv/config');
Run Code Online (Sandbox Code Playgroud)
或者
require('dotenv').config();
Run Code Online (Sandbox Code Playgroud)
或者
连接文件中的任何此类导入(根据您使用的包)。
| 归档时间: |
|
| 查看次数: |
18141 次 |
| 最近记录: |