我正在构建CRUD应用程序,我能够读取和删除数据,但我无法为特定路由更新数据库表中的数据,而且当我试图更新id为1的表中的数据时,它会更新所有id中的数据
app.get('/doctor/edit/:id', async (req, res) => {
const id = req.params.id;
//console.log(id);
const data = await doctor.getDoctorById(id);
res.render('editDoctor', { data });
});
//update doctor
app.post('/doctor/edit/:id', async (req, res) => {
const data = req.body;
const id = req.params.id; // Get the 'id' from the URL parameters
console.log(data);
console.log(id);
try {
await doctor.updateDoctor(data, id); // Pass the 'id' to the updateDoctor function
res.redirect('/doctor');
} catch (error) {
console.log('Error updating patient:', error);
res.status(500).send('Internal Server Error');
}
});
// function to update the information
const updateDoctor = async (updatedData) => {
const { id, name, specialization, contact, appointment } = updatedData;
const query = `
UPDATE doctor
SET name = $2, specialization = $3, contact = $4, appointment = $5
WHERE id = $1`;
const values = [id, name, specialization, contact, appointment]; // Correct order of values
await pool.query(query, values);
};
字符串
我在前端使用ejs,我希望现有的医生信息应该更新与此代码,但我收到的数据,但它没有在数据库中更新。
2条答案
按热度按时间oymdgrw71#
字符串
8dtrkrch2#
尝试在常量查询中使用反引号,并尝试为查询方法执行添加excpeiton处理块,即(error,results)=> {}。
也就是说,
字符串