【问题标题】:Null value in column "title" of relation "blog" violates not-null constraint关系“博客”的“标题”列中的空值违反非空约束
【发布时间】: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.contentBlognullundefined 但是在您的数据库中,您将此设置为外键,不能是 NULL
  • CREATE TABLE 博客(blog_id 序列主键 contentBlog VARCHAR (500) UNIQUE NOT NULL);
  • 如你所说的未定义
  • @dailycoding - 很好。我已经添加了一个答案让你接受其他正在寻找的人:-)

标签: postgresql express node-pg-pool


【解决方案1】:

您正在尝试将 nullundefined 变量插入您的 postgresql 表中。

检查req.body.contentBlog的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 2021-12-06
    • 2023-03-13
    • 1970-01-01
    • 2016-02-14
    相关资源
    最近更新 更多