1

I created a new Node, Express, Mongo app but suddenly I'm getting this error with Mongoose when setting up MongoDB.

Server Listening to Port: 9001 
C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\auth\scram.js:130
username = username.replace('=', '=3D').replace(',', '=2C');
                   ^

TypeError: Cannot read property 'replace' of undefined

 at executeScram (C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\auth\scram.js:130:24)
 at C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\auth\scram.js:299:7
 at _combinedTickCallback (internal/process/next_tick.js:73:7)
 at process._tickCallback (internal/process/next_tick.js:104:9) [nodemon] app crashed - waiting for file changes before starting...

my code for setting up MongoDB is:

const session    = require('express-session');
const mongoose   = require('mongoose');
const mongoStore = require('connect-mongo')(session);

// Local connection
let mongoConnectionLocal = { 
    'url': `mongodb://${process.env.MongoDBLocalUser}:${process.env.MongoDBLocalPassword}@127.0.0.1:27017/my-database`
};    

mongoose.connect(mongoConnectionLocal.url, {auth:{authdb:"admin"}}, err => { if(err) { console.log(err); }});

This is so weird because this code work with Mongoose version 4.13.2 and lower (I tried uninstalling version 5.0.16 then installed the version 4.13.2 and it works without error). Has there any changes in implementing MongoDB connection with Mongoose?

Update: Someone said use the default, Using the default creates this error:

 default: mongoose.connect(mongoConnectionLocal.url, err => { if(err) { console.log(err); }});

{ MongoError: Authentication failed.
at C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:598:61
at authenticateStragglers (C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:516:16)
at Connection.messageHandler (C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:552:5)
at emitMessageHandler (C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:309:10)
at Socket.<anonymous> (C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:452:17)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:551:20)
name: 'MongoError',
message: 'Authentication failed.',
ok: 0,
errmsg: 'Authentication failed.',
code: 18,
codeName: 'AuthenticationFailed' }
(node:20124) UnhandledPromiseRejectionWarning: Unhandled promise rejection 
(rejection id: 1): MongoError: Authentication failed.
  • 2
    The underlying `MongoClient` will attempt authentication on `admin` **by default**. There should be no need to actually include that in the options for a connection. It should only be needed if you created users in another database, and you really "should" be defining all users within the `admin` database, and simply granting permissions on other databases as required. – Neil Lunn Apr 26 '18 at 00:19
  • 1
    Option should be `{ authSource: 'admin' }` anyway. But as stated, you should really not need this at all. The "change" is in the underlying core driver where `mongodb:///` actually now identifies the "authorization" database with the `` part. What mongoose **now** does is remove your `my-database` from the connection string and then switches the selected database in code before returning the response. This is how all drivers do it now. – Neil Lunn Apr 26 '18 at 00:22
  • The thing is it is working properly on lower version of Mongoose such as version 4.13.2, so authentication on admin has been set by default on Mongoose 5.0.16? Thank you. – Sherwin Ablaña Dapito Apr 26 '18 at 00:24
  • Different "core driver" between mongoose versions. Changed from 2.x to 3.x. Various options are no longer supported. But "again", You do not need to specify "admin" as the authorization source. Authentication on **admin** has **always** been the default. – Neil Lunn Apr 26 '18 at 00:25
  • Nope I tried using { authSource: 'admin' } or removing {auth:{authdb:"admin"}} and it creates additional error. Removing {auth:{authdb:"admin"}} create authentication error and while using "authSource" makes my connection invalid. – Sherwin Ablaña Dapito Apr 26 '18 at 00:29
  • And where is the try to **actually remove the options altogether** which is what I have been telling you all along. Besides "a different error" is exactly that. Perhaps you should show what your "different error" actually is. – Neil Lunn Apr 26 '18 at 00:32
  • When you [edit your question](https://stackoverflow.com/posts/50033010/edit) to actually include this new information **please do not post screenshots**. The stuff on your console is called "text", just like everything we are typing here is. Copy and past the "text" and **not** the image. – Neil Lunn Apr 26 '18 at 00:34
  • Okay so that's "Authentication Failed". Show the actual set up of the user you are attempting to connect with. [`db.getUser()`](https://docs.mongodb.com/manual/reference/method/db.getUser/) will do that, and don't worry as it will not share the password. My money is on that the user is actually set up in the wrong database, but if you show the output of that command we can tell you for sure. – Neil Lunn Apr 26 '18 at 00:47
  • Hey, everything works with earlier version of mongoose using the "same credentials" and "same option" posted above, so there's no problem with my credentials. Actually removing the "option" on earlier version (4.13.2) also creates authentication error but thank you for your response. I guess I'll just have to use the earlier version of mongoose so this errors won't appear until Mongoose devs updated it. – Sherwin Ablaña Dapito Apr 26 '18 at 00:53
  • 2
    I think what you are missing here is that your "earlier" options is actually incorrect and ignored. Your user is therefore likely **incorrectly** created in the `my-database` namespace, which again I have been telling you all along. Likely `{ authSource: 'my-database' }` works just fine. But really you should be correcting where you have your users defined. – Neil Lunn Apr 26 '18 at 00:55

1 Answers1

2

This issue have been resolve by removing the username and password in the connection URI

instead of setting up mongoose connection this way,

const session    = require('express-session');
const mongoose   = require('mongoose');
const mongoStore = require('connect-mongo')(session);

// Local connection
let mongoConnectionLocal = { 
    'url':`mongodb://${process.env.MongoDBLocalUser}:${process.env.MongoDBLocalPassword}@127.0.0.1:27017/my-database`
};    

mongoose.connect(mongoConnectionLocal.url, {auth:{authdb:"admin"}}, err => { if(err) { console.log(err); }});

Create an option and set the database username and password there,

var options = {
  auth: {authdb: 'admin'},
  user: process.env.MongoDBLocalUser,
  pass: process.env.MongoDBLocalPassword,
}

let mongoConnectionLocal = { 
    'url': `mongodb://127.0.0.1:27017/my-database`
}; 

mongoose.connect(mongoConnectionOnline.url, options, err => { if(err) { console.log(err); }}); 

And it works.

NOTE: Someone said in the comment that I'm using a wrong credentials but I used the same credential for older and newer version of mongoose and both works.

Others are also experiencing this issue. See: MongoDB Auth Fails to find username on Bitnami MEAN Stack Image

[Reference] https://github.com/Automattic/mongoose/issues/4891