最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MongoParseError: Invalid connection string - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited May 26, 2019 at 3:35 Jack Bashford 44.1k11 gold badges55 silver badges82 bronze badges asked Feb 16, 2019 at 8:22 Vishwa SaiVishwa Sai 1111 gold badge1 silver badge6 bronze badges
Add a comment  | 

7 Answers 7

Reset to default 16

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

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:

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

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

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))

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.

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))

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
发布评论

评论列表(0)

  1. 暂无评论