0

I am totally new to nodejs. I tried to create a basic Insertion app using express and mongo db. But each time an error is throwing out

AssertionError [ERR_ASSERTION]: null == { MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]

Index.js

var express = require('express');
var router = express.Router();
var mongo = require('mongodb').MongoClient; 
var objectId = require('mongodb').ObjectID;
var assert = require('assert');
var url = 'mongodb://localhost:27017/test'; 
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index');
});  
router.post('/insert', function(req, res, next) {
     var item = {
        title: req.body.title,
        content: req.body.content,
        author: req.body.author
     };
     mongo.connect(url, function(err, db) {
     assert.equal(null, err);
     db.collection('user-data').insertOne(item, function(err, result) {
     assert.equal(null, err);
     console.log('Item inserted');
     db.close();
   });
 });
 res.redirect('/');
}); 
module.exports = router;

My form file

<form action="/insert" method="post">
    <div class="input">
        <label for="title">Title</label>
        <input type="text" id="title" name="title">
    </div>
    <div class="input">
        <label for="content">Content</label>
        <input type="text" id="content" name="content">
    </div>
    <div class="input">
        <label for="author">Author</label>
        <input type="text" id="author" name="author">
    </div>
    <button type="submit">INSERT</button>
</form>

I am using express handlebar. Currently I have installed mongodb in my local machine. Do i need to use mongoose in order to make it work. Even this should work without mongoose too. I am not able to find the error.

Udit Gogoi
  • 675
  • 2
  • 11
  • 28
  • Possible duplicate of [ECONNREFUSED error when connecting to mongodb from node.js](https://stackoverflow.com/questions/20386464/econnrefused-error-when-connecting-to-mongodb-from-node-js) – Mika Sundland Dec 03 '17 at 13:14

2 Answers2

1

I have a feeling its happening because of the following lines of code:

router.get('/', function(req, res, next) {
  res.render('index');
}); 

Its missing next()

The middleware is perhaps not letting the program run any code that comes after it.

Maybe changing it as follows will help:

router.get('/', function(req, res, next) {
  res.render('index');
  next();
});
nishkaush
  • 1,512
  • 1
  • 13
  • 20
  • just wondering if mongodb is running? *** ./mongo/bin/mongod --dbpath ~/mongo-data-new *** Do you have a command similar to this running in your terminal – nishkaush Dec 03 '17 at 14:37
  • Yah That was the problem. My mongod path wasn't set correctly. No its working after I reset the path – Udit Gogoi Dec 04 '17 at 06:16
0

What platform at you running? It seems that the Mongo Database is just not running. You can test with telnet localhost 27017 and it should establish a connection with the default MongoDB port.

Nic Cottrell
  • 9,401
  • 7
  • 53
  • 76