Edit and delete function for my app is not working

69 Views Asked by At

I am working in node js environment with handlebars and sequelize orm. Using method over ride module

const methodOverride = require('method-override');
app.use(methodOverride('_method'));

I am trying to edit and delete my project buckets for management system, which is not working. I read about the updates in handlebars security at text which helped me in fetching all my project buckets to page but i have no idea how will i use this solution for edit and delete as it requires me to pass the object first. Following are my dependencies:

"body-parser": "^1.20.2",
"express": "^4.18.2",
"express-handlebars": "^7.1.2",
"method-override": "^3.0.0",
"mysql2": "^3.7.0",
"sequelize": "^6.35.2",

EDIT CONTROLLER

async function handleEditBucket(req,res){
  const bucketID = req.params.id;
  const {title,details} = req.body;

    const bucket = await bucketModel.findOne({
      where: {TM_BUCKET_ID: bucketID}
    });
    
    if(bucket){
      bucket.TM_BUCKET_NAME = req.body.title;
      bucket.TM_BUCKET_DESCRIPTION = req.body.details;
      await bucket.save();
      res.redirect('bucket',{bucket})
      }
    else {
      res.render('error', { message: 'Bucket not found' });
    }
}

ROUTE

router.get('/edit/:bucketID', (req,res)=>{
    res.render('../views/bucket/edit')
})
router.put('/edit/:bucketID', bucketController.handleEditBucket)

EDIT FORM BUTTON IN VIEWS

<form method="post" action="/bucket/{{bucketID}}?_method=PUT">
        <input type="hidden" name="_method" value="PUT">
        <a href="/bucket/edit/{{bucketID}}" type="button" class="btn btn-outline-secondary btn-small"><span class="material-icons small-icon">edit</span></a>
      </form>

I have tried to check if i had a problem with using the id in the models.

0

There are 0 best solutions below