gpt4 book ai didi

javascript - 创建用户API Mysql自增bug

转载 作者:行者123 更新时间:2023-11-29 05:56:48 25 4
gpt4 key购买 nike

我试图让一个简单的创建用户函数运行,但不断收到一个奇怪的 sql 错误,我假设这是由于我使用 mysql 的自动递增键时出现的某种错误,但无法识别实际问题。

下面是我的服务器代码示例:

app.post('/createPhysician', function(req, res) {
console.log(req.body);
// do input checking here
var createPromise = interact.createPhysician(req.body.physicianID,
req.body.firstName,
req.body.lastName,
req.body.yearNum,
req.body.position,
req.body.isAttending,
req.body.highRiskTrained)

//did promise
createPromise.then(function(createResponse) {
res.status(200).json({"status":"success"}); // .end because we just want to return an empty response
}).catch(function(err) {
console.log(err);
res.status(500).json(err);
});
});

下面是函数的 interact.sql 代码:

createPhysician: function(
firstName,
lastName,
yearNum,
position,
isAttending,
highRiskTrained) {
var qry = "insert into Physician (firstName, lastName, yearNum, position, isAttending, highRiskTrained) values \
('"+firstName+"', '"+lastName+"', "+yearNum+", '"+position+"', "+isAttending+", "+highRiskTrained+");";
return runQuery(qry);
}

下面是我的 mysql 表的描述: enter image description here

下面是我在 Postman 中输入的内容:

{
"firstName": "firstName",
"lastName": "lastName",
"yearNum": 1,
"position": "coder",
"isAttending": 1,
"highRiskTrained": 0
}

以下是我在 Postman 中收到的请求错误:

{
"code": "ER_BAD_FIELD_ERROR",
"errno": 1054,
"sqlMessage": "Unknown column 'coder' in 'field list'",
"sqlState": "42S22",
"index": 0,
"sql": "insert into Physician (firstName, lastName, yearNum, position, isAttending, highRiskTrained) values ('undefined', 'firstName', lastName, '1', coder, 1);"
}

请求正文的 API 控制台日志输出如下:

{ firstName: 'firstName',
lastName: 'lastName',
yearNum: 1,
position: 'coder',
isAttending: 1,
highRiskTrained: 0 }

最佳答案

用函数包装查询时,确保查询使用正确的参数并正确地传递给函数。

对最终不过是打字错误的问题进行正式回答总是感觉有点奇怪,但我想你永远不知道谁可能需要指出相同类型的打字错误。

关于javascript - 创建用户API Mysql自增bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48693584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com