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.

1

2 Answers 2

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();
});
Sign up to request clarification or add additional context in comments.

2 Comments

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
Yah That was the problem. My mongod path wasn't set correctly. No its working after I reset the path
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.