I am having trouble using the PUT method to update data based on req.params.id. My approach involves retrieving data by id, displaying it in a table format, allowing users to make changes, and then updating the database with the new values.
Here is the code snippet:
router.put('/:id' , (req,res, next) => {
Student.findById(+req.params.id)
.then(data => {
let arr = data.dataValues;
res.render('edit', {
files : arr
})
})
.catch(err => {
res.status(404).send('something went wrong');
})
const theKey = key => key || undefined
const {first_name, last_name, email } = req.body
let obj = {
first_name : theKey(first_name),
last_name: theKey(last_name),
email: theKey(email),
createdAt: new Date(),
updatedAt: new Date()
}
Student.update(obj,
{ returning: true,
where: {
id : req.params.id
}
})
.then(updated => {
res.send(`updated`)
})
})
Snippet from my app.js:
app.use('/students/edit', editstudent )
However, I am encountering an issue where data does not get updated when navigating back to the student list in the database. Could there be an error in my PUT method implementation?