Im trying to connect the database in my nodejs project.
I have created a mongo account and I have chosen Coonect your application to your cluster using MonngoDB's native drivers.
So the code to connect with the database is like this:
db.js
const mongoose = require("mongoose");
const connectDB = async () => {
const conn = await mongoose
.connect(
"mongodb+srv://username:<password>#cluster0.xk55obw.mongodb.net/?retryWrites=true&w=majority",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
)
.then(() => {
console.log("Database Connected");
})
.catch((err) => console.log(err));
};
module.exports = connectDB;
When I run the project in my terminal I get this error:
Error: querySrv ECONNREFUSED _mongodb._tcp.cluster0.xk55obw.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (node:dns:213:19) {
errno: undefined,
code: 'ECONNREFUSED',
syscall: 'querySrv',
hostname: '_mongodb._tcp.cluster0.xk55obw.mongodb.net'
}
The username and password are correct, I have checked them.
How can I solve this error?
Related
I'm trying to make a request to postgreSQL from a Node and Express server on localhost. But I keep getting this error Error: connect ECONNREFUSED 127.0.0.1:5432 when trying to open localhost:8000/todos - not sure why.. I changed the username to 'postgres' (which is the defualt username), password, host, database name and port are all correct.. I can check the todos using SELECT * FROM todos; however, the app just breaks and gives me that code. I made sure that the server is running. I started it in services, and I can check the content of todos (which I manually added) in the SQL shell. Any ideas on why this is happening? I've gone thru all the other pages with similar issue, but I'm pretty new to postgreSQL overall and I'm unable to find an answer. I'm not using docker.
this is the exact error in the console:
Server runnnig on PORT 8080
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1487:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
main server.js
const PORT = 8080;
const express = require("express");
const app = express();
const pool = require("./db");
app.get("/todos", async (req, res) => {
try {
const todos = await pool.query("SELECT * FROM todos");
res.json(todos.rows);
} catch (err) {
console.error(err);
}
});
app.listen(PORT, () => console.log(`Server runnnig on PORT ${PORT}`));
db.js file where the connection is coming from
const Pool = require("pg").Pool;
require("dotenv").config();
const pool = new Pool({
user: "postgres",
password: process.env.PASSWORD,
host: "localhost",
port: 5432,
database: "todoapp",
});
module.exports = pool;
Hy, I start learning a nodejs with restapi. But I'm trying to connect my mongodb atlas but it is giving me that error:
MongoParseError: Invalid connection string
at parseConnectionString (D:\Working\nodeJS\node_modules\mongodb\lib\core\uri_parser.js:573:21)
at connect (D:\Working\nodeJS\node_modules\mongodb\lib\operations\connect.js:281:3)
at D:\Working\nodeJS\node_modules\mongodb\lib\mongo_client.js:259:5
at maybePromise (D:\Working\nodeJS\node_modules\mongodb\lib\utils.js:692:3)
at MongoClient.connect (D:\Working\nodeJS\node_modules\mongodb\lib\mongo_client.js:255:10)
at D:\Working\nodeJS\node_modules\mongoose\lib\connection.js:835:12
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (D:\Working\nodeJS\node_modules\mongoose\lib\connection.js:832:19)
at D:\Working\nodeJS\node_modules\mongoose\lib\index.js:351:10
at D:\Working\nodeJS\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (D:\Working\nodeJS\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (D:\Working\nodeJS\node_modules\mongoose\lib\index.js:1149:10)
at Mongoose.connect (D:\Working\nodeJS\node_modules\mongoose\lib\index.js:350:20)
at Object.<anonymous> (D:\Working\nodeJS\mongo.js:5:10)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
monogo.js
const mongoose = require('mongoose')
require('dotenv').config()
mongoose.Promise = global.Promise
mongoose.connect('process.env.MONGOURUI',{
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex:true,
useFindAndModify:false,
})
.then(() => console.log('MongoDB Connected...'))
.catch((err) => console.log(err))
**MONGOURUI=mongodb+srv://nodejspassword:nodejspassword#cluster0.qsis4.mongodb.net/nodeApi?retryWrites=true&w=majority;
**
const mongoose = require('mongoose')
require('dotenv').config()
mongoose.Promise = global.Promise
mongoose.connect(process.env.MONGOURI,{
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex:true,
useFindAndModify:false,
})
.then(() => console.log('MongoDB Connected...'))
.catch((err) => console.log(err))
You just need to remove single quotes from process.env.MONGOURI and it will work!
and make sure that the MONGOURI variable name is the same in the .env file.
In order to connect to the atlas, make sure you have whitelisted your IP address. you can find the deleted steps here
at QueryReqWrap.onresolve [as oncomplete] (dns.js:203:19) {
errno: 'ETIMEOUT',
code: 'ETIMEOUT',
syscall: 'queryTxt',
hostname: 'scheduly.1eln0.mongodb.net'
}
I’m facing the above connection Error while connecting with MongoDB Atlas. I’ve double-checked my username and password. I’ve whitelisted all the IP’s. I’m stuck in the middle of a project and cannot connect to the DB itself.
my connection strings are:
const mongoose = require("mongoose");
const mongodb = require("mongodb");
const uri = "mongodb+srv://ghulamghousdev:***********#scheduly.1eln0.mongodb.net/scheduly?retryWrites=true&w=majority
mongoose
.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("Connected");
})
.catch((err) => console.log(err));
The name of the Database on MongoDB atlas is scheduly.
Have you tried to add the ip of your VPS, host to the IP Whitelist of Network access?
You can see more details for that setting from here https://docs.atlas.mongodb.com/security-whitelist/
trying to connect mongoose to my express app and getting this error:
[nodemon] starting `node index.js`
Listening to the port 5000
connect ECONNREFUSED 35.156.235.216:27017
Im connecting the app directly to MongoDB Atlas, everything was working fine yesterday but this morning i got this Error. Here is the db connection file:
const mongoose = require("mongoose");
require("dotenv").config();
const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
});
console.log("connected to MongoDB..");
} catch (err) {
console.log(err.message);
}
};
I have created the cluster on the MongoDB Atlas and now trying to create the db connect to it using the connection string provided on the Atlas.
My Connection String on key.js file:
module.exports = {
//mongoURI: "mongodb://DEVELOPER:DEV1234#devconnector"
mongoURI:
"mongodb+srv://USERNAME:PWD#devconnector-7dd46.mongodb.net/test?retryWrites=true"
};
Server.js file, DB Connect:
const db = require("./config/keys").mongoURI;
mongoose
.connect(db, { useNewUrlParser: true })
.then(() => console.log("MongoDBConnected"))
.catch(err => console.log(err));
Error message:
{ Error: querySrv ESERVFAIL
_mongodb._tcp.devconnector-7dd46.mongodb.net
at errnoException (dns.js:55:10)
at QueryReqWrap.onresolve [as oncomplete] (dns.js:243:19) code: 'ESERVFAIL', errno: 'ESERVFAIL', syscall: 'querySrv', hostname:
'_mongodb._tcp.devconnector-7dd46.mongodb.net' }
Just to add, I am getting the same error message while on trying to connect with Atlas from the Compass. Any help would be appreciated!