英文:
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 '.env' 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't help me . Can you help me?
</details>
# 答案1
**得分**: 0
您的`replace`函数看起来有问题(阅读函数文档[在此处][1])
```javascript
const DB = process.env.DATABASE.replace(
/(<PASSWORD>)/,
process.env.DATABASE_PASSWORD
);
而不是
const DB = process.env.DATABASE.replace(
''<PASSWORD>'',
process.env.DATABASE_PASSWORD
);
英文:
Your replace
function looks wrong (read function documentation here)
const DB = process.env.DATABASE.replace(
/(<PASSWORD>)/,
process.env.DATABASE_PASSWORD
);
instead of
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论