gpt4 book ai didi

javascript - API 代码未运行正确的查询

转载 作者:行者123 更新时间:2023-11-29 18:01:05 28 4
gpt4 key购买 nike

我一直在尝试让我的网站调用此 API 函数,但它调用了错误的查询,即使它在我的文件中正确显示。服务端js代码如下:

app.post('/createPhysician/', function(req, res) {
console.log("below is the req body for createPhysician");
console.log(req.body);

var createPromise = interact.createPhysician(
req.body.firstName,
req.body.lastName,
req.body.yearNum,
req.body.position,
req.body.isAttending,
req.body.highRiskTrained);

createPromise.then(function(createResponse) {
res.json("successful"); // returns the physicianID for the createUsers
}).catch(function(err) {
console.log(err);
console.log(req.body);
res.json("Terrible job you botched it");
});
});

调用查询的交互文件如下:var mysql = require('mysql');

var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "password",
database: "dbName"
});

con.connect(function(err) {
if (err) {
console.error('Error:- ' + err.stack);
return;
}
console.log('Connected Id:- ' + con.threadId);
});


createPhysician: function(physicianID,
firstName,
lastName,
yearNum,
position,
isAttending,
highRiskTrained) {

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

当我在 postman 中使用以下数据运行它时,我收到一个错误,该字段甚至不再出现在我的查询中。

API 主体:

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

postman 错误消息:

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

enter image description here

下面是我的 runQuery 代码:

function runQuery(queryString) {
return new Promise(function(resolve, reject) {
con.query(queryString, function (err, result, fields){
if (err) reject(err);
resolve(result);
});
});
}

最佳答案

我认为你的问题是由 SQL 语法引起的。试试这个:

var qry = "insert into Physician (firstName, lastName, yearNum, position, isAttending, highRiskTrained) values ('"+firstName+"', '"+lastName+"', "+yearNum+", '"+position+"', '"+isAttending+"', '"+highRiskTrained+"');";

关于javascript - API 代码未运行正确的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48334312/

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