【发布时间】:2021-11-03 03:19:10
【问题描述】:
我正在尝试通过节点 js / expressjs 将表单数据从 react 发送到 mysql 数据库 现在我什么都没有得到没有错误没有数据
这是服务器代码:
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.post("/api/insert", (req, res) => {
const userName = req.body.userName;
const userDOB = req.body.userDOB;
const userEmail = req.body.userEmail;
const userPassword = req.body.userPassword;
const sqlInsert =
"INSERT INTO tblUser (strName,dtmDOB,strEmail,strPassword) VALUES (?,?)";
db.query(
sqlInsert,
[userName, userDOB, userEmail, userPassword],
(err, result) => {
if (err) throw err;
console.log("record added");
}
);
});
这是我的客户端代码:
const [userName, setUserName] = useState("");
const [userPassword, setUserPassword] = useState("");
const [userEmail, setEmail] = useState("");
const [userDOB, setUserDOB] = useState("");
const submitForm = () => {
Axios.post("http://localhost:4000/api/insert", {
userName: userName,
userDOB: userDOB,
userEmail: userEmail,
userPassword: userPassword,
}).then(() => {
alert("success");
});
是 axios 还是 express 的问题? 知道出了什么问题吗?
【问题讨论】:
-
我不知道有一种方法可以将“INSERT”sql 语句的值指定为参数,并让 SQL 组装它们。我总是用内联的所有变量组装字符串(在字符串的“VALUES”部分。)
标签: mysql node.js reactjs express