0

I can't figure out where I missed to add something. I get the following error: Cannot GET /portal/destroybox/5797318673cf3f581163455c when I click on delete or update icons in portal.ejs:

(createbox in portal.ejs works and creates the item and stores it in the db, and then I forEach in the table to show all items with update and delete icons behind each item. Also the // todo test part in server.js is for another part of my app and that works, nothing to do with the portal/box part. I also do not have route in server.js for createbox, but that part works, so why do I need it for destroybox if I need it at all?)

portal.ejs

<table class="table"> <thead> <tr> <th>Name</th> <th>Vm</th> <th>Update</th> <th>Delete</th> </tr> </thead> <tbody> <% boxes.forEach( function ( box ){ %> <tr> <td> <%= box.box_name %> </td> <td> <%= box.vm %> </td> <td> <a class="fa fa-wrench" aria-hidden="true" href="/portal/editbox/<%= box._id %>" title="Update this box"></a> </td> <td> <a class="fa fa-trash-o" aria-hidden="true" href="/portal/destroybox/<%= box._id %>" title="Destroy this box"></a> </td> </tr> <% }); %> </tbody> </table> <p> <strong>Add new box to `available boxes`</strong> <br> </p> <form action="/createbox" method="post"> <div class="form-group"> <label>Box name</label> <input type="text" class="form-control" name="box_name"> </div> <div class="form-group"> <label>VM</label> <input type="text" class="form-control" name="vm"> </div> <div class="form-group"> <label>Description</label> <input type="text" class="form-control" name="description"> </div> <button type="submit" class="btn btn-warning btn-lg">Add box</button> </form> 

server.js

// server.js // set up ====================================================================== // get all the tools we need // mongoose setup var db = require('./config/database.js'); require('./app/models/db'); require('./app/models/box'); var express = require('express'); var http = require('http'); var path = require('path'); var engine = require('ejs-locals'); var favicon = require('serve-favicon'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var methodOverride = require('method-override'); var logger = require('morgan'); var errorHandler = require('errorhandler'); var static = require('serve-static'); var app = express(); var port = process.env.PORT || 8080; var mongoose = require('mongoose'); var passport = require('passport'); var flash = require('connect-flash'); var session = require('express-session'); var routesindex = require('./routes/index'); //var routesbox = require('./routes/boxi'); // configuration =============================================================== mongoose.connect(db.url); // connect to our database require('./config/passport')(passport); // pass passport for configuration // set up our express application app.engine('ejs', engine); app.use(logger('dev')); // log every request to the console app.use(cookieParser()); // read cookies (needed for auth) app.use(bodyParser()); // get information from html forms app.use(favicon(__dirname + '/public/favicon.ico')); app.use(methodOverride()); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); // set up ejs for templating // required for passport app.use(session({ secret: 'secretkeykeykeykey' })); // session secret app.use(passport.initialize()); app.use(passport.session()); // persistent login sessions app.use(flash()); // use connect-flash for flash messages stored in session // routes ====================================================================== var routes = require('./app/routes/routes')(app, passport); // load our routes and pass in our app and fully configured passport // todo test app.use(routesindex.current_user); app.get('/ind', routesindex.ind); app.post('/ind/create', routesindex.create); app.get('/ind/destroy/:id', routesindex.destroy); app.get('/ind/edit/:id', routesindex.edit); app.post('/ind/update/:id', routesindex.update); app.use(static(path.join(__dirname, 'public'))); // launch ====================================================================== app.listen(port); console.log('The magic happens on port ' + port); 

routes.js

// ===================================== // PORTAL ============================== // ===================================== // show the signup form app.get('/portal', isLoggedIn, function (req, res) { var user_id = req.cookies ? req.cookies.user_id : undefined; Box. find({ user_id: user_id }). sort('-updated_at'). exec(function (err, boxes) { if (err) return next(err); res.render('portal', { boxes: boxes }); }); }); app.post('/createbox', isLoggedIn, function (req, res) { new Box({ user_id: req.cookies.user_id, box_name: req.body.box_name, vm: req.body.vm, description: req.body.description, updated_at: Date.now() }).save(function (err, box, count) { if (err) return next(err); res.redirect('/portal/'); }); }); app.get('/destroybox/:id', isLoggedIn, function (req, res) { Box.findById(req.params.id, function (err, box) { var user_id = req.cookies ? req.cookies.user_id : undefined; if (box.user_id !== user_id) { return utils.forbidden(res); } box.remove(function (err, box) { if (err) return next(err); res.redirect('/portal/'); }); }); }); app.get('/editbox/:id', isLoggedIn, function (req, res) { var user_id = req.cookies ? req.cookies.user_id : undefined; Box. find({ user_id: user_id }). sort('-updated_at'). exec(function (err, boxes) { if (err) return next(err); res.render('editbox', { title: 'Vagrant Box', boxes: boxes, current: req.params.id }); }); }); app.post('/updatebox/:id', isLoggedIn, function (req, res) { Box.findById(req.params.id, function (err, box) { var user_id = req.cookies ? req.cookies.user_id : undefined; if (box.user_id !== user_id) { return utils.forbidden(res); } box.box_name = req.body.box_name; box.vm = req.body.vm; box.description = req.body.description; box.updated_at = Date.now(); box.save(function (err, box, count) { if (err) return next(err); res.redirect('/portal/'); }); }); }); 
1
  • Hi thanks for the reply, solution given by Jai works. Hehe, wrong path. Commented Jul 26, 2016 at 10:22

1 Answer 1

1

Because you don't have routes for that as the error states:

app.get('/destroybox/:id' 

You can see you have a route from root / to destroybox then :id etc. While you are having a href of:

 href="/portal/destroybox/<%= box._id %>" 

from root / to portal and then destroybox then the id. Which is no where defined in the config.


Solution is to change the href to:

href="/destroybox/<%= box._id %>" 

same goes for other href attributes too.

Sign up to request clarification or add additional context in comments.

2 Comments

big bloated question, nice concise answer :).
Hehe, I think I tried that before, but did not work. It works now hehe thanks for the solution. Will mark as solution, need to wait 4 minutes tho.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.