javascript - Failing to delete entry from MongoDB database using Node.JS Express Jade -
for life of me cannot seem find solution this. every tutorial , resource got doesn't seem help. want delete entry mongodb database can't seem able jade view, database router , database want. want able delete entry database.jade view , route it, not view nor router. when run code below keep getting 404 error route it's trying access.
my app.js file below:
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 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 employee app.use('/database/#{entry._id}?/delete', database); // catch 404 , forward error handler app.use(function(req, res, next) { var err = new error('not found'); err.status = 404; next(err); }); // error handlers // development error handler // 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 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");
my database.js (this handles routing database view enter entries, view entries , want delete them) code below:
var express = require('express'); var router = express.router(); var mongoose = require('mongoose'); var schema = mongoose.schema; /* database stuff */ //connect data store , set database var db = mongoose.connection; //connect database mongoose.connect('mongodb://localhost/mandela_diaries/data'); //create model connects schema , entries collection in mandela_diaries database var entry = mongoose.model("entry", new schema({date: 'date', link: 'string'}), "entries"); mongoose.connection.on("open", function() { console.log("mongodb connected!"); }); //the route getting data database - form router.get("/", function(req, res) { //send current entries 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 posting data 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 deleting data database - delete/remove todo item id // changed path `/` `/:id` router.delete('/:id', function (req, res) { entry.findbyid(req.params.id) .exec(function(err, entries) { // changed `if (err || !doc)` `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;
and code database view below:
extends layout block content .container .row .col-s-12 h1 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 year' yy") text - 'day' d 'of' mm 'in 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
Comments
Post a Comment