MongoNetworkError: connection 0 to localhost:27017 timed out .(didn't understand previous solutions)

Viewed 1132

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}`); 
});
0 Answers
Related