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.