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