1

I'm building a e-commerce page using express and when i want to delete one of my products everything seems to work fine but the url doesn't change (and it should).

So here is my code:

//route

router.use('/product', require('./product.routes'))

router.delete('/item/:id', productController.deleteProduct);

//controller

 deleteProduct: (req, res) => {
        let id = req.params.id;
        let newDatabase = database.filter((item) => item.id != id);
        fs.writeFileSync(path.join(__dirname, '../database/productos.json'), JSON.stringify(newDatabase, null, 4), {encoding: 'utf-8'});
        let productVisited = newDatabase.filter(item=>item.category=="visited");
        let productInSale = newDatabase.filter(item=>item.category=="in-sale");
        res.render("home", {newDatabase, productVisited, productInSale });
    }
}

//EJS

<form method="POST" action="/product/item/<%= database.id %>?_method=DELETE">
<input type="submit" class="action-button delete" value="ELIMINAR">
</form>

//ENTRY POINT (APP.JS)

const express = require('express');
const path = require('path');
const methodOverride = require ('method-override');
const app = express();
app.use(express.static(path.join(__dirname, "./public")));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(methodOverride("_method"));
app.set("view engine", "ejs");
app.set("views",path.join(__dirname, "./src/views"));
app.use("/", require ("./src/routes/index.routes"));
app.use((req, res, next) => {
    res.status(404).render('error')
});
app.listen(process.env.PORT || 3000, () => {
    console.log('Server Running')
})

Thanks in advance for your time! have a nice day

Mukul Kumar Jha
  • 1,062
  • 7
  • 19
  • What do you mean by URL does not change? Can you paste the expected and acutal output? – Sohan Nov 16 '21 at 09:52
  • It is not clear what you are trying to achieve. Your "delete" controller renders the "home" view. What else do you want it to do? Maybe you want to redirect instead of rendering "home"? – Sefi R Nov 16 '21 at 10:00
  • @Sohan i mean that i'm in the details of a product (./product/item/:id) and when I press delete i'm redirected to the home page where i've my product list and the deleted product its gone (so everything works fine) but the url it should be /.home (cos i'm in the home) but insted the url remains as ./product/item/:id?_method=DELETE – Nicolas Martin Gomez Nov 16 '21 at 10:18
  • @SefiR the controller delete a product and render the home page where the deleted product it's gone (this work fine). i can't redirect cos i'm sending an object so i need to render (¿right?) Everything is working as expected, the issue is the url that it should be /.home (cos i'm in the home) but insted the url remains as ./product/item/:id?_method=DELETE – Nicolas Martin Gomez Nov 16 '21 at 10:21
  • [res.redirect()](http://expressjs.com/en/5x/api.html#res.redirect) isn't enough? – Xeelley Nov 16 '21 at 10:21
  • @Xeelley can't res.redirect() cos i'm rendering a .ejs with a object .json that's the list of products minus the deleted one – Nicolas Martin Gomez Nov 16 '21 at 10:26
  • check this, it had similar problem. I dont thins you can use render and url will change when used ejs templates https://stackoverflow.com/questions/62882153/url-does-not-change-with-res-render – Sohan Nov 16 '21 at 10:45
  • In this case you should user redirect and pass the data as query string. Look here for a good explanation: https://stackoverflow.com/questions/19035373/how-do-i-redirect-in-expressjs-while-passing-some-context – Sefi R Nov 16 '21 at 10:54

0 Answers0