我使用node
和mysql
来构建rest api,但是当我使用POST
方法时,我得到 Column' xxx'不能为空。我不明白我哪里出错了。我在其他一些网站上尝试过,但我并不理解。
app.post('/info', function(req,res) {
connection.query('INSERT into task(id,title,status) values(?,?,?)',
[req.body.id, req.body.title, req.body.status], function(error, rows, fields) {
if (error) {
console.log(error);
} else {
res.json(rows);
}
})
});
请让我知道我哪里出错了。任何帮助/建议都非常感谢。
答案 0 :(得分:0)
查看您的SQL架构,或用于在表中定义列的语句。有一个列已定义为NOT NULL,并且此INSERT语句不会将任何数据POST到定义为NOT NULL的列,这就是您收到该错误的原因。您必须将数据发布到列' xxx' 。
如果不需要使用每个查询的数据初始化列' xxx' ,则可以通过消除NOT NULL约束来解决此问题。