Error "Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://""

Hello,
I have an error that blocks me, have you ever encountered this error?

File .env =>

DB_CONNECT = 'mongodb+srv://ID:PASSWORD@URL.mongodb.net/?retryWrites=true&w=majority';

index.js

const dotenv = require("dotenv").config();
mongoose.set("strictQuery", false);

mongoose.connect(process.env.DB_CONNECT, {

    useUnifiedTopology: true,

    useNewUrlParser: true,

}).then(console.log('connect sucess to mongodb'))

bot.ticketTranscript = mongoose.model('transcripts',

    new mongoose.Schema({

        Channel : String,

        Content : Array

    })

)

Error =>

throw new MongoParseError('Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"');

MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"

Thanks you in advance

1 Like

Hello @bill, Welcome to the MongoDB community forum,

Can you please make sure by consol print this variable process.env.DB_CONNECT has the correct connection string?

Hi, thanks you for answer,

console display

'mongodb+srv://ID:PASSWORD@URL.mongodb.net/?retryWrites=true&w=majority';

With ’ ';

It’s normal ?

It looks good, check out the documentation, I think you are missing something debug your code step by step,
https://mongoosejs.com/docs/connections.html

Out of the question, If you are using mongoose latest version then don’t need to pass useUnifiedTopology: true, and useNewUrlParser: true in connection because by default it set true

it not

remove the quotes and if it still does not work remove leading and trailing spacss

1 Like

The trailing semicolon is probably erroneous too.

1 Like

[MONGOOSE] DeprecationWarning: Mongoose: the strictQuery option will be switched
back to false by default in Mongoose 7. Use mongoose.set('strictQuery', false); if you want to prepare for this change. Or use mongoose.set('strictQuery', true); to suppress this warning.

Invalid scheme, expected connection string to start with “mongodb://” or “mongodb+srv://”

both the error will be gone
first use the
// mongoose.set(‘strictQuery’, true) in top
and remove the extra space in the link of mondodb

remove the ‘;’ from the last of .env file, and it will work

1 Like

why are you storing string value in env??? and also why ‘;’
This should be-

DB_CONNECT = mongodb+srv://ID:PASSWORD@URL.mongodb.net/?retryWrites=true&w=majority

Thanks your suggestion worked. It was this removing ‘;’ semicolon at the end.

bro which line of the code remove the '" ; " plz tell me

The line which is having your connect string.Remove semicolon at the end of connect string
Please scroll to top of this thread where you see DB_CONNECT = ‘your_connect_string’; <------------ remove this semicolon

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.