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

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.

[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