使用Express从Node.js后端发布路由时发生内部服务器错误

时间:2018-11-18 17:00:56

标签: mysql node.js express

我是nodejs的新手,想学习如何连接到我的localhost上的现有数据库并将tuples添加到其中。我决定选择nodejs作为后端,express来帮助设置路由。当我尝试执行POST请求时,出现错误ER_BAD_FIELD_ERROR: Unknown column 'firstName' in 'field list'。我已经重新启动数据库和nodejs应用程序,检查以确保我的name字段具有正确的拼写,还检查了我的routes在{{1}中是否相同}。我将放置form.htmlform.html的{​​{1}}代码。

node.js

POST

node.js

app.post("/usercreate", (req, res) => {
  console.log("first name is " + req.body.create_fname);
  console.log("last name is " + req.body.create_lname);

  const firstName = req.body.create_fname;
  const lastName = req.body.create_lname;

  const connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    database: "employees"
  });

  const queryString =
    "INSERT INTO departments(firstName, lastName) VALUES(?,?)";
  connection.query(
    queryString,
    [firstName, lastName],
    (err, results, fields) => {
      if (err) {
        console.log("Could not post" + err);
        res.sendStatus(500);
        return;
      }
      console.log("Inserted new department with dept_name");
      res.end();
    }
  );
});

0 个答案:

没有答案