title: Failing to delete entry from MongoDB database using Node.JS Express Jade
body: <p>For the life of me I cannot seem to find a solution to this. Every tutorial and resource I got doesn't seem to help. All I want to do is delete an entry from my MongoDB database but I can't seem to be able to get the Jade view, the database router and the database to do what I want. Essentially I want to be able to delete an entry from my database.jade view and route back to it, not to another view nor another router. When I run the code below I keep getting a 404 error for the route it's trying access. </p>
  
  <p>My app.js file is below:</p>
  
  <pre><code>var express = require('express');
  var path = require('path');
  var favicon = require('serve-favicon');
  var logger = require('morgan');
  var cookieParser = require('cookie-parser');
  var bodyParser = require('body-parser');
  var router = express.Router();
  
  var routes = require('./routes/index');
  var users = require('./routes/users');
  var database = require('./routes/database');
  var methodOverride = require('method-override');
  
  var app = express();
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'jade');
  
  // uncomment after placing your favicon in /public
  //app.use(favicon(__dirname + '/public/favicon.ico'));
  app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use('/public', express.static(__dirname + '/public'));
  app.use(express.static(path.join(__dirname, 'public')));
  app.use('public/js', express.static(path.join(__dirname + 'public/js')));
  app.use('public/css', express.static(path.join(__dirname + 'public/css')));
  
  app.use('/', routes);
  app.use('/database', database);
  app.use('/create', database);
  app.use('/delete', database);
  app.use('/:id', database);
  app.use(methodOverride('_method'));
  //delete an employee
  app.use('/database/#{entry._id}?/delete', database);
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  });
  
  // error handlers
  
  // development error handler
  // will print stacktrace
  if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
      res.status(err.status || 500);
      res.render('error', {
        message: err.message,
        error: err
      });
    });
  }
  
  // production error handler
  // no stacktraces leaked to user
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: {}
    });
  });
  
  
  module.exports = app;
  
  app.listen(8080);
  
  console.log("The server has started");
  </code></pre>
  
  <p>My database.js (this handles the routing for the database view where I enter the entries, view the entries and where I want to delete them) code is below: </p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;
  
  /* Database stuff */
  //connect to the data store and the set up the database
  var db = mongoose.connection;
  
  //connect to the database
  mongoose.connect('mongodb://localhost/Mandela_Diaries/data');
  
  //Create a model which connects to the schema and entries collection in the Mandela_Diaries database
  var Entry = mongoose.model("Entry", new Schema({date: 'date', link: 'string'}), "entries");
  
  mongoose.connection.on("open", function() {
      console.log("mongodb is connected!");
  });
  
  //The route for getting data for the database - GET form
  router.get("/", function(req, res) {
      //Send the current entries to the page
      Entry.find({}, function(err, entries) {
          console.log(entries);
          if(err) {
              res.status(404).json({"error": "not found", "err":err});
              return;
          } else {
              res.render('database', {title: 'database', entries: entries});
          }
      });
  
  });
  
  //The route for posting data to the database - POST
  router.post('/', function(req, res) {
      var newEntry = new Entry(req.body);
      newEntry.save(function(err, entries){
          if (err !== null) {
              res.status(500).json(err);
          } else {
              res.redirect('database');
          };
      });
  });
  
  //The route for deleting data to the database - Delete/remove todo item by its id
  // Changed path from `/` to `/:id`
      router.delete('/:id', function (req, res) {
          Entry.findById(req.params.id)
              .exec(function(err, entries) {
                 // changed `if (err || !doc)` to `if (err || !entries)`
                  if (err || !entries) {
                      res.statusCode = 404;
                      res.send({});
                  } else {
                      entries.remove(function(err) {
                          if (err) {
                              res.statusCode = 403;
                              res.send(err);
                          } else {
                              res.send({});
                          }
                      });
                  }
              });
      });
  
  module.exports = router;
  </code></pre>
  
  <p>And my code for the database view is below:</p>
  
  <pre><code>extends layout
  
  block content
      .container
        .row
          .col-s-12
            h1 The Mandela Diaries Database
        .row
          .col-s-4
            h3 Add Entry
        .row
          .col-s-12
            form.form-inline(method='post', action='/create')
              .form-group
                label(for='date') Date: 
                input#datepicker.form-control.datepicker(type='text', name='date')
              .form-group
                label(for='link') Link: 
                input#link.form-control(type='string', name='link')
              button.btn.btn-default(type='submit') Submit
        .row
          p
                | Format options:
                br
                select#format
                  option(value='mm/dd/yy') Default - mm/dd/yy
                  option(value='yy-mm-dd') ISO 8601 - yy-mm-dd
                  option(value='d M, y') Short - d M, y
                  option(value='d MM, y') Medium - d MM, y
                  option(value='DD, d MM, yy') Full - DD, d MM, yy
                  option(value="'day' d 'of' MM 'in the year' yy") With text - 'day' d 'of' MM 'in the year' yy
        br
        ul#dbentries.col-s-12
      each entry in entries
        p= 'Date: ' + entry.date + ': Link: ' + entry.link + ' '
            span
            form(method='POST', action='/database/#{entry._id}?_method=DELETE')
              button(type='submit') Delete
        .row
  </code></pre>