I am building a simple app using node, express and pug. We are working to store "bycicles" data. We have a create/view and delete views examples but were asked to implement the update function given the examples.
I can load the specified bycicle data in the form, but, when pressing send as to update the written data for the bycicle a 404 error loads instead, Im veeeery new to this webpage programming and have no idea how this is happening, so I'm asking for any help in understanding the issue.
I used the create.pug example to build my update.pug view. In the form I am a bit unsure if I should put (action='bicicletas/update') or ('bicicletas/'+bici.id+'/update') I will post it here (the update.pug):
extends ../layout
block content
a(href='/bicicletas') Volver
br
br
br
h1 Editar bicicleta
br
br
// form(action='bicicletas/'+bici.id+'/update',method='post')
form(action='bicicletas/update', method='post')
label(for='id') ID:
input#id(type='text' name='id' value=bici.id )
br
select(name='color', id='', required)
each val in ['--select--','Blanco' , 'Negro' , 'Rojo','Verde']
option=val
br
select(name='modelo', id='', required)
each val in ['--select--','Ruta' , 'Cross' , 'Montian','Turismo']
option=val
br
label(for='lat') Latitud:
input#id(type='text' name='lat' value= bici.ubicacion[0] )
br
label(for='lng') Longitud:
input#id(type='text' name='lng' value= bici.ubicacion[1] )
br
br
input.btn(type='submit' name='OK' )
I have succesfully completed the "get" function but the "post" function is the one that's throwing the 404.
In my routes > bycicles I have set up my routes, as indicated by the example, in this way:
const express = require("express");
const router = express.Router();
const bicicletaController = require("../controllers/bicicleta");
router.get("/", bicicletaController.list);
router.get("/:id/show", bicicletaController.show);
router.get("/create", bicicletaController.create_get);
router.post("/create", bicicletaController.create_post);
router.get("/:id/update", bicicletaController.update_get);
router.post("/:id/update", bicicletaController.update_post);
router.post("/:id/delete", bicicletaController.delete);
module.exports = router;
In a controllers folder I added an example bycicle script in which I created an update_get and an update_post using the other ones as reference
const Bicicleta = require("../models/bicicleta");
exports.list = function (re, res) {
res.render("bicicletas/index", { bicis: Bicicleta.allBicis });
};
exports.show = function (req, res) {
var bici = Bicicleta.findById(req.params.id);
res.render("bicicletas/show", { bici });
};
exports.create_get = function (req, res) {
res.render("bicicletas/create");
};
exports.create_post = function (req, res) {
var bici = new Bicicleta(req.body.id, req.body.color, req.body.modelo);
bici.ubicacion = [req.body.lat, req.body.lng];
Bicicleta.add(bici);
res.redirect("/bicicletas");
};
exports.delete = function (req, res) {
Bicicleta.removeById(req.body.id);
res.redirect("/bicicletas");
};
exports.update_get = function (req, res) {
var bici = Bicicleta.findById(req.params.id);
res.render("bicicletas/update", { bici });
};
exports.update_post = function (req, res) {
var bici = new Bicicleta(req.body.id, req.body.color, req.body.modelo);
bici.ubicacion = [req.body.lat, req.body.lng];
Bicicleta.update(req.body);
res.redirect("/bicicletas");
};
I created a bycicle model in which I added the update using a similar method to the preexisting removebyID function, using a splice to replace the bycicle.
let Bicicleta = function (id, color, modelo, ubicacion) {
this.id = id;
this.color = color;
this.modelo = modelo;
this.ubicacion = ubicacion;
};
Bicicleta.prototype.toString = function () {
return `id: ${this.id} | color: ${this.color}`;
};
Bicicleta.allBicis = [];
Bicicleta.add = function (aBici) {
Bicicleta.allBicis.push(aBici);
};
Bicicleta.findById = function (aBiciId) {
var aBici = Bicicleta.allBicis.find((x) => x.id == aBiciId);
if (aBici) return aBici;
else throw new Error(`No existe una Bicicleta con el id: ${aBiciId}`);
};
Bicicleta.removeById = function (aBiciId) {
var aBici = Bicicleta.findById(aBiciId);
for (let i = 0; i < Bicicleta.allBicis.length; i++) {
if (Bicicleta.allBicis[i].id == aBiciId) {
Bicicleta.allBicis.splice(i, 1);
break;
}
}
};
Bicicleta.update = function (aBici) {
var aBiciOld = Bicicleta.findById(aBici.id);
for (let i = 0; i < Bicicleta.allBicis.length; i++) {
if (Bicicleta.allBicis[i].id == aBiciOld.id) {
Bicicleta.allBicis.splice(i, 1, aBici);
break;
}
}
};
And as I just mentioned I'm very confused as why the 404 error. The console doesn't give any hints, just the error handler.
NotFoundError: Not Found
at E:\2021-2\Topicos_Ingenieria_Software\LAB_01\redBicicletas\app.js:29:8
at Layer.handle [as handle_request] (E:\2021-2\Topicos_Ingenieria_Software\LAB_01\redBicicletas\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (E:\2021-2\Topicos_Ingenieria_Software\LAB_01\redBicicletas\node_modules\express\lib\router\index.js:317:13)
at E:\2021-2\Topicos_Ingenieria_Software\LAB_01\redBicicletas\node_modules\express\lib\router\index.js:284:7
at Function.process_params (E:\2021-2\Topicos_Ingenieria_Software\LAB_01\redBicicletas\node_modules\express\lib\router\index.js:335:12)
at next (E:\2021-2\Topicos_Ingenieria_Software\LAB_01\redBicicletas\node_modules\express\lib\router\index.js:275:10)
at E:\2021-2\Topicos_Ingenieria_Software\LAB_01\redBicicletas\node_modules\express\lib\router\index.js:635:15
at next (E:\2021-2\Topicos_Ingenieria_Software\LAB_01\redBicicletas\node_modules\express\lib\router\index.js:260:14)
at Function.handle (E:\2021-2\Topicos_Ingenieria_Software\LAB_01\redBicicletas\node_modules\express\lib\router\index.js:174:3)
at router (E:\2021-2\Topicos_Ingenieria_Software\LAB_01\redBicicletas\node_modules\express\lib\router\index.js:47:12)
All help is very much appreciated.
I found the mistake. The part in the form
form(action='bicicletas/update', method='post')
should really be
form.inline(action=`/bicicletas/${bici.id}/update`, method='post')
That fixed the 404 error.