3

I'm completely new with this backend thing, this is my code and when i'm trying to connect the mongoose to the database this error is showing.

I've searched thoroughly and found this example [MongoError: connection 0 to localhost:27017 timed out but I didn't understand how to use this in my project.

{ MongoNetworkError: connection 0 to localhost:27017 timed out
at Socket.<anonymous> 


(C:\Users\ABCD\Pictures\Freelancer\node_modules\mongodb- 
core\lib\connection\connection.js:259:7)

at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at Socket.emit (events.js:208:7)
at Socket._onTimeout (net.js:422:8)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }

THIS IS MY CODE

const express = require('express');
const app = express();
const exphbs = require('express-handlebars'); 
const mongoose = require('mongoose');
//HANDLEBAR MIDDLEWARE
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
//Connect to mongoose
mongoose.connect('mongodb://localhost/freelancer-dev',{
useNewUrlParser: true
})
.then(()=>console.log('mongodb connected'))
.catch(err=> console.log(err));

const port = process.env.PORT ||5000;
app.listen(port,()=>{
console.log(`listening at port${port}`); 
});
MDIPANJAN
  • 115
  • 2
  • 9
  • it needs investigation on server. mongoose tries to connect to db and times out. something's wrong with mongodb. seems like Windows Firewall blocking connection. go to control panel and find rule regarding mongodb and allow connections to it – num8er Nov 16 '18 at 07:26
  • I've downloaded MongoDB 4.0.4 msi package on windows 10, and while i was installing it i think i got some error and skipped that part. Do i need to install It from the scratch again? – MDIPANJAN Nov 16 '18 at 07:34
  • try to install it again. maybe will help – num8er Nov 16 '18 at 07:39
  • While I'm trying to re install the MongoDB service , i'm getting this prompt as an error. service 'MongoDB server'(mongodb) failed to start verify that you have sufficient privileges to start system services. – MDIPANJAN Nov 16 '18 at 08:16
  • run install as administrator – num8er Nov 16 '18 at 08:35
  • 1
    Thank you. I re installed the MongoDB and at last got the right thing : [nodemon] restarting due to changes... [nodemon] restarting due to changes... [nodemon] starting `node app.js` listening at port5000 mongodb connected :p – MDIPANJAN Nov 16 '18 at 10:40

0 Answers0