[英]I am trying to connect mongodb server with node
I tried to connect my mongo cluster with my local server but this error keeps on showing up.我试图将我的 mongo 集群与我的本地服务器连接,但这个错误一直出现。 I am following a tutorial and it seems to work fine for the tutor but this error comes for me.我正在学习一个教程,它似乎对导师来说效果很好,但这个错误是为我而来的。 I have provided the error screenshot below.我在下面提供了错误屏幕截图。
The src has been provided已提供 src
const express = require('express'); const env = require('dotenv'); const app = express(); const bodyParser = require('body-parser'); const mongoose = require('mongoose'); //routes const userRoutes = require('./routes/user'); //constants env.config(); //mongodb connect //mongodb+srv://root:<password>@cluster0.9ylhh.mongodb.net/myFirstDatabase?retryWrites=true&w=majority mongoose.connect( `mongodb+srv://${process.env.MONGO_DB_USER}:${process.env.MONGO_DB_PASSWORD}@cluster0.9ylhh.mongodb.net/${process.env.MONGO_DB_DATABASE}?retryWrites=true&w=majority`, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true } ).then(() => { console.log('Database connected'); }); //middleware app.use(express.urlencoded({ extended: true})); app.use(express.json()); app.listen(process.env.PORT, () => { console.log(`server is running on port ${process.env.PORT}`); });
I also did create a .env file with the credentials details and stuff我还创建了一个包含凭据详细信息和内容的 .env 文件
I tried to make a comment instead of an answer, but I don't have enough reputation.我试图发表评论而不是回答,但我没有足够的声誉。
If the error is authentication, maybe you have a problem with your credentials.如果错误是身份验证,则可能是您的凭据有问题。
Does your username or password have any of these chars?您的用户名或密码是否有这些字符? : / ? # [ ] @
If they do, you'll have to URI encode them like so:如果他们这样做,您将必须像这样对它们进行 URI 编码:
${encodeURIComponent(process.env.MONGO_DB_USER)}
${encodeURIComponent(process.env.MONGO_DB_PASSWORD)}
More info here: https://docs.mongodb.com/manual/reference/connection-string/#examples更多信息: https : //docs.mongodb.com/manual/reference/connection-string/#examples
BTW: You forgot the env
part on your ${process.MONGO_DB_PASSWORD}
.顺便说一句:您忘记了${process.MONGO_DB_PASSWORD}
上的env
部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.