创建User API Mysql自动增量错误

时间:2018-02-08 19:45:45

标签: javascript mysql api

我试图让一个简单的创建用户函数运行,但不断得到一个奇怪的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);
    });
});

下面是func的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 }

1 个答案:

答案 0 :(得分:2)

使用函数包装查询时,请确保查询使用正确的参数并将其正确传递到函数中。

总是觉得有点奇怪,对最终只是一个错字的正式回答,但我想你永远不知道谁可能需要同样的错字指出。