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
Related
while trying to connect with my mongoDB atlas using mongoose connect method in node js I'm getting below mentioned parser error, I've checked my DB credentials and there are no special characters (:, /, ?, #, [], #) present in it.
Error:
MongoParseError: URI malformed, cannot be parsed
Code:
const mongoose = require('mongoose');
const DB = process.env.DB.replace('<password>', process.env.DB_PASSWORD);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then((con) => {
console.log(con.connections);
console.log('DB connection successful!');
});
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?
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/
I've tried connecting a MongoDB cloud atlas using URL in my Node application,but getting the following error:-
UnhandledPromiseRejectionWarning: Error: queryTxt ETIMEOUT cluster0-coypu.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:206:19)
Ive connected using:-
mongoose.connect(process.env.MONGODB_URI || config.connectionString, { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true });
mongoose.Promise = global.Promise;
In order to check whether the connection is established or not ive donw using:-
mongoose.connect('config.connectionString',{
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true
}).then(
() => {
console.log("Database connection established!");
},
err => {
console.log("Error connecting Database instance due to: ", err);
}
);
where config.connectionString contains the URL generated in my atlas.
mongoose
.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
log.info("successfully connected to db");
})
.catch((err) => {
log.error("db connection failed " + err);
});
Just make sure the URI is in correct format
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);
}
};