This the updating router part of the route ‘/parts’
// Update a part
router.put('/:id', async (req, res) => {
try {
console.log("data got",req.body);
const [rows, fields] = await pool.query(
'UPDATE partimages SET partname = ?, description = ?, company = ? WHERE id = ?',
[
req.body.partname, req.body.description, req.body.company, req.params.id
]);
res.json(rows);
} catch (err) {
console.error(err);
res.status(500).send('Server Error');
}
});
I am sending data from this function:
function UpdatePartfunc(formData, id) {
fetch(`http://localhost:3000/parts/${id}`, {
method: 'PUT',
body: formData
})
.then(response => response.json());
}
var id = '6';
const formData = {
id: '6',
name: 'Test Part',
description: 'This is a test part',
company: 'Test Company',
};
UpdatePartfunc(formData, id);
I am trying to send the formdata from the frontend UpdatePartfunc to the API so that it can update the table data using the id.
Log of the API:
Listening at http://localhost:3000
data got: undefined
TypeError: Cannot read properties of undefined (reading 'partname')
Log of Frontend function:
PUT http://localhost:3000/parts/6 500 (Internal Server Error)
UpdatePartfunc @ try.js:2
(anonymous) @ App.jsx:14
VM9480:1 Uncaught (in promise) SyntaxError: Unexpected token 'S', "Server Error" is not valid JSON
New contributor
Apsan XD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.