首页 > 解决方案 > UnhandledPromiseRejectionWarning: MongoParseError: URI 格式错误,无法解析

问题描述

const mongoose = require('mongoose');
require("dotenv").config();

// mongoose connection
const conn = process.env.DB_CONNECTION;
const connection = mongoose.createConnection(conn , {useUnifiedTopology : true , useNewUrlParser : true});

// mongoose schema
// the schemas followed


// mongoose models
const User = mongoose.model("User" , userRegistrationSchema);
const Meme = mongoose.model("Meme" , memeSchema);

// export that model
module.exports = {connection , User , Meme};

我不是想连接到 mongodb atlas,而是连接到本地主机本身。

.env 文件:

SECRET=somesecret,
DB_CONNECTION=mongodb://localhost:27017/memeDB


(node:18052) UnhandledPromiseRejectionWarning: MongoParseError: URI malformed, cannot be parsed
at parseConnectionString (E:\Angela YU web dev\PROJECT_MEME\node_modules\connect-mongo\node_modules\mongodb\lib\core\uri_parser.js:568:21)    
at connect (E:\Angela YU web dev\PROJECT_MEME\node_modules\connect-mongo\node_modules\mongodb\lib\operations\connect.js:282:3)
at E:\Angela YU web dev\PROJECT_MEME\node_modules\connect-mongo\node_modules\mongodb\lib\mongo_client.js:225:5
at maybePromise (E:\Angela YU web dev\PROJECT_MEME\node_modules\connect-mongo\node_modules\mongodb\lib\utils.js:685:3)
at MongoClient.connect (E:\Angela YU web dev\PROJECT_MEME\node_modules\connect-mongo\node_modules\mongodb\lib\mongo_client.js:221:10)
at Function.MongoClient.connect (E:\Angela YU web dev\PROJECT_MEME\node_modules\connect-mongo\node_modules\mongodb\lib\mongo_client.js:445:22)
at new MongoStore (E:\Angela YU web dev\PROJECT_MEME\node_modules\connect-mongo\build\main\lib\MongoStore.js:110:46)    at Function.create (E:\Angela YU web dev\PROJECT_MEME\node_modules\connect-mongo\build\main\lib\MongoStore.js:136:16)
at Object.<anonymous> (E:\Angela YU web dev\PROJECT_MEME\app.js:25:23)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:18052) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated 
either by throwing inside of an async function without a catch block, or by rejecting a promise which 
was not handled with .catch(). To 
terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled- 
rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:18052) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the 
future, promise rejections that are not handled will terminate the Node.js process with a non-zero 
exit code.

有人可以解释错误是什么以及如何解决它吗?我使用 mongoose 和 mongodb 已经有一段时间了,但从未遇到过这样的错误

标签: node.jsmongodbexpressmongooseuri

解决方案


推荐阅读