1

I'm building a little blog using express js for the backend, but I'm facing an issue: I get this error in the console

null value in column "contentblog" of relation "blog" violates not-null constraint

Code:

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'
            })
        }
    });
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    I need more information but it looks like the `req.body.contentBlog` is `null` or `undefined` however in your database you have this set up as a foreign key which cannot be `NULL` – Ewan Jan 01 '21 at 19:31
  • CREATE TABLE blog ( blog_id serial PRIMARY KEY contentBlog VARCHAR ( 500 ) UNIQUE NOT NULL ); – dailycoding Jan 01 '21 at 19:33
  • its undefined as u said – dailycoding Jan 01 '21 at 19:53
  • Does this answer your question? [req.body is undefined - expressjs with postgres database](https://stackoverflow.com/questions/65533660/req-body-is-undefined-expressjs-with-postgres-database) – marc_s Jan 01 '21 at 22:00
  • @dailycoding - okay great. I've added an answer for you to accept for others who are looking too :-) – Ewan Jan 02 '21 at 10:05

1 Answers1

0

You are trying to insert a null or undefined variable into your postgresql table.

Check the value of req.body.contentBlog.

Ewan
  • 14,592
  • 6
  • 48
  • 62