在微信小程序中调用node启的本地服务中的增加/修改接口时候,返回状态码500,node服务端返回以下错误:
服务端代码如下:
// 更改数据接口
app.put('/update/:id', (req, res) => {
const id = req.params.id;
const { name, age, title } = req.body;
const sql = `UPDATE test1 SET name = ${name}, age = ${age} ,title = ${title} WHERE id = ${id}`;
connection.query(sql, (err, result) => {
if (err) {
console.error('Error updating data in the database: ', err);
res.status(500).send('Error updating data in the database');
return;
}
res.send('Data updated successfully');
});
});
小程序中代码如下:
const res = await wx.p.request({
url: 'http://localhost:3000/update/' + 2,
method: 'put',
header: {
"Content-Type": "application/x-www-form-urlencoded" //用于post
},
data: {
age: 98,
title:"田",
name:'田八'
},
})
console.log(res);
mysql表如下:
错入原因如下:
表中的name和title字段 是非 int 类型
在写sql语句时,需要加 ’ ’