MongoParseError: Invalid connection string
Asked Answered
D

7

11

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.

Durwin answered 16/2, 2019 at 8:22 Comment(0)
L
16

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

Levulose answered 19/3, 2019 at 16:5 Comment(1)
For me mongodb://mongo/ worked. where mongo is the docker service name.Talanta
C
8

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:

Cherellecheremis answered 9/4, 2020 at 17:24 Comment(0)
L
4

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

mongoose.connect('mongodb://localhost:27017/User',{ useNewUrlParser: true })
Larrabee answered 5/10, 2019 at 9:15 Comment(0)
M
2

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))
Maccarthy answered 11/1, 2020 at 19:22 Comment(0)
H
0

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.

Heiduc answered 26/5, 2019 at 3:2 Comment(0)
K
0

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))
Kosak answered 3/10, 2021 at 19:6 Comment(0)
R
0

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
Rube answered 21/1, 2023 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.