trouble connecting to mongodb - node.js

I am following a tutorial and trying to connect to mongodb, i have two files
the first is server.js with the following codes
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
//body parser middleware
app.use(bodyParser.json());
//DB config
const db = require('./config/keys').mongoURI;
//connect to mongo
mongoose
.connect(db,{ useNewUrlParser: true })
.then(()=>console.log('Connected to mongodb...'))
.catch(err=>console.log(err));
const port = process.env.PORT || 5000;
app.listen(port,()=>console.log(`server connected on port ${port}`));
The second is the config file
module.exports = {
mongoURI:'mongodb+srv://denis:password#shoppingapp-wmnbw.mongodb.net/shopping?retryWrites=true&w=majority'
}
what I am trying to achieve is to show on the console connected to mongodb...
instead I am given this error message
server connected on port 5000
(node:35030) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
MongoNetworkError: failed to connect to server [shoppingapp-shard-00-01-wmnbw.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to shoppingapp-shard-00-01-wmnbw.mongodb.net:27017 closed
at TLSSocket.<anonymous> (/home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/connection.js:372:9)
at Object.onceWrapper (events.js:417:26)
at TLSSocket.emit (events.js:310:20)
at net.js:672:12
at TCP.done (_tls_wrap.js:557:7)]
at Pool.<anonymous> (/home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/topologies/server.js:438:11)
at Pool.emit (events.js:310:20)
at /home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/pool.js:561:14
at /home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/pool.js:1008:9
at callback (/home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/connect.js:97:5)
at /home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/connect.js:124:7
at _callback (/home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/connect.js:349:5)
at Connection.errorHandler (/home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/connect.js:365:5)
at Object.onceWrapper (events.js:417:26)
at Connection.emit (events.js:310:20)
at TLSSocket.<anonymous> (/home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/connection.js:370:12)
at Object.onceWrapper (events.js:417:26)
at TLSSocket.emit (events.js:310:20)
at net.js:672:12
at TCP.done (_tls_wrap.js:557:7)
I have tried to change the driver version but still get the errors, where am I going wrong?

It turns out I was not connecting with the right IP address.

Related

Mongoose does not connect to local database

Previously I tried to connect it by setting dbURI to the URI that mongo gives you upon clicking "connect application" and it did not work, later I tried to set up a local database with mongoDB, where i downloaded it, set it up and all the like. I created the data/db folder as well and i can open up an empty database upon typing the link in compass. Regardless of all of that, mongoose still doesn't allow me to connect to the database, giving me this error:
MongoServerError: bad auth : Authentication failed.
at MessageStream.messageHandler (D:\tests\node_modules\mongodb\lib\cmap\connection.js:467:30)
at MessageStream.emit (node:events:527:28)
at processIncomingData (D:\tests\node_modules\mongodb\lib\cmap\message_stream.js:108:16)
at MessageStream._write (D:\tests\node_modules\mongodb\lib\cmap\message_stream.js:28:9)
at writeOrBuffer (node:internal/streams/writable:389:12)
at _write (node:internal/streams/writable:330:10)
at MessageStream.Writable.write (node:internal/streams/writable:334:10)
at TLSSocket.ondata (node:internal/streams/readable:754:22)
at TLSSocket.emit (node:events:527:28)
at addChunk (node:internal/streams/readable:315:12) {
ok: 0,
code: 8000,
codeName: 'AtlasError'
}
Here's my code:
const express = require('express');
const mongoose = require('mongoose');
const CPD = require("./database/log");
// express
const app = express();
// database
const dbURI = 'mongodb://localhost:27017/local'
mongoose.connect(dbURI).then((res) => console.log("success")).catch((err) => console.log(err))
//serverstuff
app.get('/', (req, res) => {
res.send('Welcome!')
})
app.listen(3000);
try using '127.0.0.1' instead of 'localhost'
const dbURI = 'mongodb://127.0.0.1:27017/local'

Deprecated body-parser?

I am getting the error message deprecated body-parser i've looked up other methods concerning parsing the data but none seem to work. the below code what ive got
const express = require('express');
const app = express();
const Task = require('./api/models/ListModels.js'); //created model loading here
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
// mongoose instance connection url connection
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/Tododb');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const routes = require('./api/routes/ListRoutes.js'); //importing route
routes(app); //register the route
app.listen(3000, () => {
console.log('running on 3000')
})
The error:
PS C:\Users\demar\Desktop\New folder\ListAPI> node app.js
(node:10424) 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.
running on 3000
(node:10424) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (C:\Users\demar\Desktop\New folder\ListAPI\node_modules\mongodb-core\lib\topologies\server.js:562:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (C:\Users\demar\Desktop\New folder\ListAPI\node_modules\mongodb-core\lib\connection\pool.js:316:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (C:\Users\demar\Desktop\New folder\ListAPI\node_modules\mongodb-core\lib\connection\connection.js:245:50)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
(node:10424) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without
a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:10424) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
It honestly tells you what to do..
To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
I added the options and it works perfectly..
Originally:
// Connect to Mongo:
mongoose.connect(db).then(() => console.log("Mongo DB Connected")).catch(err => console.log(err));
Current:
// Connect to Mongo:
mongoose.connect(db, { useNewUrlParser: true}).then(() => console.log("Mongo DB Connected")).catch(err => console.log(err));
This fixed the issue
The error is that your version of Node's URL string parser, that Mongoose is using, is deprecated.
Pass in the option {useNewUrlParser: true} to mongoose.connect() to use the new Node Core URL API.
mongoose.connect('mongodb://localhost/Tododb', {useNewUrlParser: true})
That error is appearing because you didn't specified the port number mongodb is running on.
mongoose.connect('mongodb://localhost:27017/databasename', { useNewUrlParser: true })
this will fix your issue :)
Yes, body-parser has been deprecated.
Don't use it anymore
Since Express 4.16+ the body parsing functionality has become built into express
You can do
app.use(express.urlencoded({extended: true}));
app.use(express.json()) // To parse the incoming requests with JSON payloads
From directly express, without having to install body-parser.
so you can uninstall body-parser using npm uninstall body-parser, and simply use the above code from express.

Mongo db Atlas 3.6.5 authentication failed

I am pretty new to node.js,am trying to write data to a free online mongo db atlas using mongoose,however i keep getting Authentication failed despite editing mongo db settings to connect from anywhere and also using correct username and password for the db user.
Here is my app.js file where i connect to mongo db
app.js:
const express = require('express');
const morgan = require('morgan');
const bodyParser= require('body-parser');
const mongoose = require('mongoose');
const app = express();
const productRoutes= require('./api/routes/products');
const ordersRoutes= require('./api/routes/orders');
mongoose.connect('mongodb+srv://hilary:'+process.env.MONGO_ATLAS_PW+'#node-rest-dnqwa.mongodb.net/test?retryWrites=true').catch(err => console.log(err));
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.use((req,res,next) =>{
res.header("Access-Control-Allow-Origin","*");
res.header('Access-Control-Allow-Headers','Origin,X-Requested-With,Content-Type,Accept,Authorization');
if(req.method === 'OPTIONS'){
res.header('Access-Control-Allow-Methods','PUT,POST,GET,DELETE,PATCH');
return res.status(200).json({});
}
next();
});
app.use('/products',productRoutes);
app.use('/orders',ordersRoutes);
app.use((req,res,next) => {
const error= new Error('not found');
error.status = 404;
next(error);
});
app.use((error,req,res,next) =>{
res.status(error.status || 500);
res.json({
error: {
message: error.message
}
})
})
module.exports = app;
i get this error in my terminal:
{ MongoError: authentication fail
at C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\topologies\replset.js:1430:15
at C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\connection\pool.js:877:7
at C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\connection\pool.js:853:20
at finish (C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\auth\scram.js:174:16)
at handleEnd (C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\auth\scram.js:184:7)
at C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\auth\scram.js:289:15
at C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\connection\pool.js:544:18
at process._tickCallback (internal/process/next_tick.js:112:11)
name: 'MongoError',
message: 'authentication fail',
errors:
[ { name: 'node-rest-shard-00-00-dnqwa.mongodb.net:27017',
err: [MongoError] },
{ name: 'node-rest-shard-00-02-dnqwa.mongodb.net:27017',
err: [MongoError] } ] }
I am certain am using correct name and password so why cant it authenticate me,server runs on my localhost
EDIT
So i followed link to docs and now connect this way:
mongoose.connect('mongodb://hilary:'+process.env.MONGO_ATLAS_PW+'#localhost:27017/test').catch(err => console.log(err));
I then get this error in terminal:
{ MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\topologies\server.js:505:11)
at Pool.emit (events.js:180:13)
at Connection.<anonymous> (C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\connection\pool.js:329:12)
at Object.onceWrapper (events.js:272:13)
at Connection.emit (events.js:180:13)
at Socket.<anonymous> (C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\connection\connection.js:245:50)
at Object.onceWrapper (events.js:272:13)
at Socket.emit (events.js:180:13)
at emitErrorNT (internal/streams/destroy.js:64:8)
at process._tickCallback (internal/process/next_tick.js:114:19)
name: 'MongoNetworkError',
message: 'failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]' }
What worked was a slight variation from the mongoose docs,it was suggested on the Atlas server,for everyone who has Mongodb 3.4+ connect using the regular mongo db driver even when using mongoose, my connection string looks like:
mongoose.connect('mongodb+srv://hilary:<password>#node-rest-dnqwa.mongodb.net').catch(err => console.log(err));

trying to connect to mongodb Atlas using nodeJs

I am working on a small project(REST API) using nodeJs + MongoDB. I have been able to install MongoDB locally and connect to it using mongoose. However for some reason, when I try to connect using MongoDB Atlas it fails. It looks like it connects but then after 2 seconds, I get an error message saying sockets closed(see error below). I have no clue what is going on. I have whitelisted my IP, checked my login info to make sure I am using the correct password and indeed I am using because I am able to connect using MongoDB compass. Any help is greatly appreciated.
My current local ENV package versions are:
nodeJs:V9.7.1
mongoose:V6.1
=== MongoDb Atlas ===
mongodb:3.6
Below is the code that I am using to connect to the database:
var express = require('express'),
port = process.env.PORT || 3000,
mongoose = require('mongoose'),
user = require('./api/models/userModel'),
config = require('./api/config');
bodyParser = require('body-parser');
var authRoutes = require('./api/routes/authRoutes'),
userRoutes = require('./api/routes/userRoutes'),
reviewRoutes = require('./api/routes/reviewRoutes');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://user:myPass#cluster0-shard-00-00 zd6jq.mongodb.net/myDb');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
//Swagger Info
var options = {
explorer : true
};
app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerDocument, options));
//END Swagger Info
//REGISTER ROUTES
userRoutes(app);
authRoutes(app);
reviewRoutes(app);
app.listen(port);
console.log('iReview RESTful API server listenning on port: ' + port);
module.exports = app;
====ERROR MESSAGE ===
/Users/mdiez/node_test/node_modules/mongodb/lib/server.js:228
process.nextTick(function() { throw err; })
^
MongoError: server cluster0-shard-00-00-zd6jq.mongodb.net:27017 sockets closed
at Pool.<anonymous> (/Users/mdiez/node_test/node_modules/mongodb-core/lib/topologies/server.js:325:47)
at Object.onceWrapper (events.js:219:13)
at Pool.emit (events.js:127:13)
at Connection.<anonymous> (/Users/mdiez/node_test/node_modules/mongodb-core/lib/connection/pool.js:101:12)
at Object.onceWrapper (events.js:219:13)
at Connection.emit (events.js:127:13)
at Socket.<anonymous> (/Users/mdiez/node_test/node_modules/mongodb-core/lib/connection/connection.js:142:12)
at Object.onceWrapper (events.js:219:13)
at Socket.emit (events.js:127:13)
at TCP._handle.close [as _onclose] (net.js:558:12)
var uri = 'mongodb://<usernamr>:<password>#<clustername>/<dbname>?ssl=true&replicaSet=<replica setname>&authSource=admin';
var db = mongoose.connect(uri).catch((error) => { console.log(error); });
Specify the replica set name, ssl true and authentication database. This is based on reference from Atlas documentation.
You can use this code to connect to Compass and Application. You just need to the following:
replace all the three primary and secondary shard from Cluster -> Overview
replace natours-app with your cluster name.
Compass:
mongodb://babar_bahadur:PASSWORD#natours-app-shard-00-00-ybksz.mongodb.net:27017,natours-app-shard-00-01-ybksz.mongodb.net:27017,natours-app-shard-00-02-ybksz.mongodb.net:27017/test?authSource=admin&replicaSet=natours-app1-shard-0&readPreference=primary&appname=MongoDB%20Compass&retryWrites=true&ssl=true
Application:
mongodb://babar_bahadur:PASSWORD#natours-app-shard-00-00-ybksz.mongodb.net:27017,natours-app-shard-00-01-ybksz.mongodb.net:27017,natours-app-shard-00-02-ybksz.mongodb.net:27017/test?ssl=true&replicaSet=natours-app-shard-0&authSource=admin&retryWrites=true

mongoose atlas connection fails while mongodb driver works

I need to connect to an Atlas replica set with mongoose. It's really weird. If I connect via the mongodb-js driver everything works fine. CRUD works like a charm.
Here's how I connect via MongoClient:
let dbURL = 'myAtlasConnectionURL';
let db = await MongoClient.connect(dbURL);
await db.collection('users').insert({name: 'username'});
let result = await db.collection('users').find({});
log.info('result', await result.toArray());
db.collection('users').remove({});
await db.close();
But if use the mongoose connection I keep getting:
MongoError: connection 5 to cluster0-shard-00-02-c4nst.mongodb.net:27017 closed
at Function.MongoError.create (/mypath/vendors/node_modules/mongodb-core/lib/error.js:29:11)
at Socket.<anonymous> (/mypath/vendors/node_modules/mongodb-core/lib/connection/connection.js:202:22)
at Object.onceWrapper (events.js:316:30)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at TCP._handle.close [as _onclose] (net.js:548:12)
Here's how I try to connect via mongoose, using the same dbURL as above:
mongoose.connect(dbURL, {
useMongoClient: true
});
I have no idea, what could possibly be wrong.

Resources