【发布时间】:2021-04-08 11:46:32
【问题描述】:
我正在为后端使用 express js 构建一个小博客,但我遇到了一个问题:我在控制台中收到此错误
关系“blog”的“contentblog”列中的空值违反了非空约束
代码:
const connection = require('../DatabaseConnection/db')
module.exports.Add = function (req, res) {
const blog = {
title: req.body.title,
contentBlog: req.body.contentBlog,
}
connection.query('INSERT INTO blog(contentBlog) values($1)', [blog.contentBlog], function (error, results, fields) {
if (error) {
res.json({
status: false,
message: 'there are some errors with the query'
})
console.log(error)
} else {
res.json({
status: true,
data: results,
message: 'Post has been added successfully'
})
}
});
}
【问题讨论】:
-
我需要更多信息,但看起来
req.body.contentBlog是null或undefined但是在您的数据库中,您将此设置为外键,不能是NULL -
CREATE TABLE 博客(blog_id 序列主键 contentBlog VARCHAR (500) UNIQUE NOT NULL);
-
如你所说的未定义
-
@dailycoding - 很好。我已经添加了一个答案让你接受其他正在寻找的人:-)
标签: postgresql express node-pg-pool