【问题标题】:Node.js post API is not working with MySQL database?Node.js post API 不适用于 MySQL 数据库?
【发布时间】:2021-09-08 05:55:39
【问题描述】:

我正在尝试使用 MySQL 数据库使用 Node.js 发布 API,API 没有给出响应,只抛出 error - code: 204

我正在使用 Postman 插入数据 -

    {
    
    "articleid":"1233wawdasyyyd4",
    "userid": "123uu"
} 

在 MySQL 表中有 4 个字段。

id(unique and incrementing) articleid (varchar) userid (varchar) datetime (current timestamp)

   var deletelog = (req, res) => {
   const articleid = req.body.articleid;
   const userid = req.body.userid;

 var sql = `INSERT INTO deletearticles_log  (articleid, userid)
   VALUES ('"+articleid+"', '"+userid+"')`;

connection.query(sql,[articleid, userid], function (error, results, fields) {
    if (error) {
      res.send({
        "code":400,
        "failed":"error ocurred",
        "error": error
      })
    }else{
      if(results.length >0){
        res.send({
          "code":200,
          result : results
        });
      }
      else{
        res.send({
          "code":204,
          "success":"Record insertion Failed!"
        });
      }
    }
  });
} 

我不知道这段代码有什么问题,如果你能帮助我,我很感激,干杯!!

【问题讨论】:

    标签: mysql node.js api


    【解决方案1】:

    为查询[articleid, userid] 传递了参数,但SQL 中没有出现任何参数。将 SQL 中的串联替换为问号。

    var sql = `INSERT INTO deletearticles_log  (articleid, userid) VALUES (?, ?)`;
    

    【讨论】:

    • 它仍然给code-204 error我已经更新了邮递员的信息请检查
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 2023-03-27
    • 2018-05-04
    • 2017-07-15
    • 1970-01-01
    相关资源
    最近更新 更多