简体   繁体   English

我正在尝试将 mongodb 服务器与节点连接

[英]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.我在下面提供了错误屏幕截图。

Error which comes up出现的错误

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.

相关问题 我正在尝试从 mongodb 连接我的 server.js express api 但它显示一些错误我不明白 - I am trying to connect my server.js express api from mongodb but it showing some error i don't understand it 我正在尝试通过 Clearone 使用 NodeJS node-telnet-client 连接到 Converge Pro 2,但我仍然无法连接 - I am trying to connect to Converge Pro 2 by Clearone using NodeJS node-telnet-client but I am still not able to connect 出现错误:querySrv ECONNREFUSED _mongodb._tcp.auctiondbcluster.s6rzg.mongodb.net。 当我离线时尝试连接到 localhost - Getting Error: querySrv ECONNREFUSED _mongodb._tcp.auctiondbcluster.s6rzg.mongodb.net. When trying to connect to localhost when I am offline 我正在尝试从Node JS / Express连接到postgres数据库。 邮递员没有回应 - I am trying to connect to postgres database from Node JS/Express. No response in Postman 我的服务器已启动,但是当我尝试连接它时,出现错误[Node.js] - My Server is up but when I try to connect to it I am getting an error [Node.js] 我正在尝试从 mongodb 服务器呈现数据以响应应用程序。 它不工作 - I am trying to render data from mongodb server to react app. And it's not working 无法使用节点 js 连接到 MSSQL 服务器数据库我正在使用包 mssql ,版本是“mssql”:“^6.2.0” - Not able to connect to MSSQL server database using the node js I am using the package mssql , version is "mssql": "^6.2.0" 如何在远程服务器(ubuntu)上的node.js中安装并连接到mongodb? - How do I install, and connect to mongodb in node.js on my remote server (ubuntu)? 尝试将node.js连接到mongodb,获得成功响应但没有连接 - Trying to connect node.js to a mongodb, getting successful response but no connection 尝试为 togetherjs 应用程序设置集线器服务器,并且我正在使用 node.js 运行服务器时出现以下错误 - Trying to set a hub server for togetherjs application and i am using node.js to run the server i get the following error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM