req.query始终导致一个空对象。
我被重新路由到一个说BAD_NULL_ER的页面,因为名称不能为空(因为我以这种方式设置了模式),但是当我刷新时,我尝试插入的项目实际上被添加到了数据库中。
这是请求
let req = new XMLHttpRequest();
let param = `name=${form.elements.name.value}&reps=${form.elements.reps.value}&weight=${form.elements.weight.value}&date=${form.elements.date.value}&lbs=${form.elements.lbs.value}`;
req.open("POST", "/?" + param, true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send("/?" + param);
这是路线
app.post('/', function (req, res, next) {
console.log(req.query);
var sql = "INSERT INTO workouts(`name`, `reps`, `weight`, `date`, `lbs`) VALUES(?, ?, ?, ?, ?)";
connection.query(sql,
[req.query.name, req.query.reps, req.query.weight, req.query.date, req.query.lbs],
(err, rows, fields) => {
if (err) {
console.log(err);
next(err);
return;
}
res.send(JSON.stringify(rows));
});
});