HankCa
HankCa

Reputation: 9629

azure cosmodb '=' in connection string fails in mongoose.connect(...)

MongoError: database name must be a string

Nodejs script:

// Azure tells me to use mongodb://mydb:[email protected]:10255/?ssl=true
// I change the = to %3D - I've confirmed this is the correct escaping
readonly serverConnection = 'mongodb://mydb:LONG-STRING-ENDING-WITH%3D%[email protected]:10255/?ssl=true');

...
const mongoDB = await mongoose.connect(
    this.serverConnection,
    {
      useNewUrlParser: true,
      reconnectTries: Number.MAX_VALUE,
      reconnectInterval: 1000
    }
  );

If I remove the == or %3D%3D, or change to something else (eg. XX) then the error is the expected:

{ MongoError: Authentication Failed

So it isn't the length of the string and must be the ==.

Why is this happening? What can one do to get around this?

Upvotes: 1

Views: 473

Answers (2)

lucamuh
lucamuh

Reputation: 1385

I found a good answer, where you do not have to relinquish the new url parser. You have to add your database name at the end of the connection string like this:

mongodb://<connectionstring/url>:port/<DATABASENAME>?ssl=True

In my opinion, this is a clean solution. I hope I could help

Upvotes: 1

HankCa
HankCa

Reputation: 9629

I found part of the answer. Remove the:

useNewUrlParser: true

And ignore the warning. For now. It tells you that this solution won't work sometime in the future.

DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

I'd appreciate if anyone can extend this answer with details on how to use the new parser and == in connection strings. And also it would be nice to know why == or an escaped version is an issue.

Upvotes: 1

Related Questions