我一直试图让我的网站调用此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);
}
当我在邮递员中使用以下数据运行它时,我收到的错误甚至不再出现在我的查询中。
API正文:
{
"firstName": "Jane",
"lastName": "Doe",
"yearNum": 1,
"position": "coder",
"isAttending": 1,
"highRiskTrained": 0
}
邮递员错误信息:
{
"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');"
}
下面是我的runQuery代码:
function runQuery(queryString) {
return new Promise(function(resolve, reject) {
con.query(queryString, function (err, result, fields){
if (err) reject(err);
resolve(result);
});
});
}
答案 0 :(得分:0)
我认为您的问题是由SQL语法引起的。试试这个:
var qry = "insert into Physician (firstName, lastName, yearNum, position, isAttending, highRiskTrained) values ('"+firstName+"', '"+lastName+"', "+yearNum+", '"+position+"', '"+isAttending+"', '"+highRiskTrained+"');";