Vishwa Sai
Vishwa Sai

Reputation: 111

MongoParseError: Invalid connection string

I am trying to connect MongoDB database with this code but when running it I get the error (see the error below after the code). The initial error was in the line where it was resolved by adding useNewUrlParser: true but even after this I still get more errors. I am using MongoDB version 4.0.1. Does anybody know how to resolve this error?

mongoose.connect('User://localhost:27017/User',{ useNewUrlParser: true })

Error while running this code:

(node:11068) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): MongoParseError: Invalid connection string (node:11068) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Upvotes: 11

Views: 62026

Answers (7)

Merrin K
Merrin K

Reputation: 1780

Try this mongoose.set('strictQuery', true);

db.js

const express = require('express');
const mongoose =require('mongoose')
const app = express();

mongoose.set('strictQuery', true);
mongoose.connect('mongodb://localhost:27017/database_name', {
    useNewUrlParser: true, 
    useUnifiedTopology: true
})
.then(() => console.log('MongoDB Connected...'))
.catch((err) => console.log(err))

app.listen(3000,()=>{ console.log("Running on port 3000") })

npm i express mongoose
node db.js

Upvotes: 0

Jose Antonio
Jose Antonio

Reputation: 898

I just added the // after the dots to indicate the localhosts, it is for mongodb 5

const mongoose = require('mongoose');

const MONGODB_HOST = 'mongodb://localhost/'
const MONGODB_DB = 'usuarios'

mongoose.connect(MONGODB_HOST,{
    useUnifiedTopology: true,
    useNewUrlParser: true

})

    .then(db => console.log('Db connection established'))
    .catch(err => console.log(err))

Upvotes: 0

Vail Williams
Vail Williams

Reputation: 161

Instead of User://localhost, use mongodb://localhost/ I had the same problem.

Upvotes: 16

Giovani
Giovani

Reputation: 175

I was receiving the same error, then I used:

mongoose.connect("mongodb://localhost:27017/[yourDbName]", {
  useUnifiedTopology: true,
  useNewUrlParser: true
});

Substitute [yourDbName] for your MongoDB database's name:

Upvotes: 8

Rafael
Rafael

Reputation: 519

Try this and it should work,

mongoose.connect('mongodb://localhost/mycargarage', {useNewUrlParser: true, useUnifiedTopology: true})
    .then(() => console.log('MongoDB Connected...'))
    .catch((err) => console.log(err))

Upvotes: 2

Ahmad Sharif
Ahmad Sharif

Reputation: 4435

The host you have written is not correct, and it should be

mongoose.connect('mongodb://localhost:27017/User',{ useNewUrlParser: true })

Upvotes: 4

Leigh277
Leigh277

Reputation: 191

I had this same issue. In my case, the issue was caused by my password. Apparently, if there are special characters in the password, you need to use the HEX value.

Upvotes: 0

Related Questions