我为什么会收到这个错误消息:“MongoParseError URI格式错误,无法解析”?

huangapple go评论78阅读模式
英文:

Why I get this error "MongoParseError URI malformed, cannot be parsed"?

问题

const mongoose = require('mongoose');
const dotenv = require('dotenv');

process.on('uncaughtException', err => {
  console.log('UNCAUGHT EXCEPTION! 🚩 Shutting down...');
  console.log(err.name, err.message);
  process.exit(1);
});

dotenv.config({ path: './.env' });
const app = require('./app');

const DB = process.env.DATABASE.replace(
  '<PASSWORD>',
  process.env.DATABASE_PASSWORD
);

console.log(DB);
mongoose
  .connect(DB, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false
  })
  .then(() => console.log('DB connection successful!'));

const port = process.env.PORT || 3000;
const server = app.listen(port, () => {
  console.log(`App running on port ${port}...`);
});

process.on('unhandledRejection', err => {
  console.log('UNHANDLED REJECTION! 🚩 Shutting down...');
  console.log(err.name, err.message);
  server.close(() => {
    process.exit(1);
  });
});

这是我的代码,我想要连接MongoDB服务器。

NODE_ENV=development
PORT=3000
DATABASE=mongodb+srv://muhosman:<PASSWORD>@cluster0.0kuwazr.mongodb.net/natours?retryWrites=true&w=majority
DATABASE_LOCAL=mongodb://localhost:27017/natours
DATABASE_PASSWORD=xxx

JWT_SECRET=my-ultra-secure-and-ultra-long-secret
JWT_EXPIRES_IN=90d
JWT_COOKIE_EXPIRES_IN=90

EMAIL_USERNAME=xxx
EMAIL_PASSWORD=xxx
EMAIL_HOST=smtp.mailtrap.io
EMAIL_PORT=25

这是我的 '.env' 文件。

我正在使用 VS Code。当我尝试连接我的 MongoDB 时,我遇到了这个错误。我搜索了这个错误,但其他帖子对我没有帮助。你能帮我吗?


<details>
<summary>英文:</summary>

const mongoose = require('mongoose');
const dotenv = require('dotenv');

process.on('uncaughtException', err => {
console.log('UNCAUGHT EXCEPTION! 💥 Shutting down...');
console.log(err.name, err.message);
process.exit(1);
});

dotenv.config({ path: './.env' });
const app = require('./app');

const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);

console.log(DB);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
})
.then(() => console.log('DB connection successful!'));

const port = process.env.PORT || 3000;
const server = app.listen(port, () => {
console.log(App running on port ${port}...);
});

process.on('unhandledRejection', err => {
console.log('UNHANDLED REJECTION! 💥 Shutting down...');
console.log(err.name, err.message);
server.close(() => {
process.exit(1);
});
});

This is my code where I want to connect MongoDB server.

NODE_ENV=development
PORT=3000
DATABASE=mongodb+srv://muhosman:<PASSWORD>@cluster0.0kuwazr.mongodb.net/natours?retryWrites=true&w=majority
DATABASE_LOCAL=mongodb://localhost:27017/natours
DATABASE_PASSWORD=xxx

JWT_SECRET=my-ultra-secure-and-ultra-long-secret
JWT_EXPIRES_IN=90d
JWT_COOKIE_EXPIRES_IN=90

EMAIL_USERNAME=xxx
EMAIL_PASSWORD=xxx
EMAIL_HOST=smtp.mailtrap.io
EMAIL_PORT=25

This is my &#39;.env&#39; file.

I am using vs code. When I want to connect my mongoDB I am getting this error. I searched this error but the other posts doesn&#39;t help me . Can you help me?

</details>


# 答案1
**得分**: 0

您的`replace`函数看起来有问题(阅读函数文档[在此处][1])

```javascript
const DB = process.env.DATABASE.replace(
  /(&lt;PASSWORD&gt;)/,
  process.env.DATABASE_PASSWORD
);

而不是

const DB = process.env.DATABASE.replace(
  '&#39;&lt;PASSWORD&gt;&#39;',
  process.env.DATABASE_PASSWORD
);
英文:

Your replace function looks wrong (read function documentation here)

const DB = process.env.DATABASE.replace(
  /(&lt;PASSWORD&gt;)/,
  process.env.DATABASE_PASSWORD
);

instead of

const DB = process.env.DATABASE.replace(
  &#39;&lt;PASSWORD&gt;&#39;,
  process.env.DATABASE_PASSWORD
);

huangapple
  • 本文由 发表于 2023年1月6日 13:47:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定