Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Failed to save data into postgres database using sequelize.js, but system return column multiple times

While trying to save data into postgres database using sequelize BlogModel.create() system failed to save the data into table and server is returning columns createdat, updatedat, multiple times in console. ( please see below ). In the scheme I have added the column only once, can someone advise on this issue here ?

Executing (default): INSERT INTO "userBlogs" ("id","email","blogdetails","tags","createdat","updatedat","createdAt","updatedAt") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING "id","email","blogdetails","tags","createdat","updatedat","createdAt","updatedAt";

//userBlogs.js

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

'use strict';
module.exports = (sequelize, DataTypes) => {
    const userBlogs = sequelize.define('userBlogs', {
        id: {
            type: DataTypes.INTEGER(10),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        email: {
            type: DataTypes.STRING(255),
            allowNull: false
        },
        blogdetails: {
            type: DataTypes.TEXT,
            allowNull: false
        },
        tags: {
            type: DataTypes.STRING(255),
            allowNull: false
        },
        createdat: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW
        },
        updatedat: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW
        }
    }, {
        timestamps: true,
        tableName: 'userBlogs'
    });

    return userBlogs;
};

//server.js

const usersBlogSchema = require('./modals/userBlogs');
  const BlogModel = usersBlogSchema(sequelize, DataTypes);


 app.post('/service/createblogs', async (req, res, next)=> {

    try {
      const userEmail = req.body.email;
      const blogDetails = req.body.blogValue;
      const tags = req.body.tagValue;

      if (Object.keys(req.body).length === 0) {
        res.status(403).json({ fail: "Invalid blog request or blog request is blank !" });
      } else {
      var requestData = {email:userEmail, blogdetails:blogDetails, tags:tags  };
      const createBlogRequest = await BlogModel.create(requestData);
       res.status(200).json({ success: true });
      }
    } catch (e) {
     console.log(e)
     return next(e);
   }
});

>Solution :

Returning createdAt and updatedAt multiple times because you have added columns (createdAt and updatedAt )and also timestamps:true ,
timestamps also adds these both columns
use either columns or timestamps

'use strict';
module.exports = (sequelize, DataTypes) => {
    const userBlogs = sequelize.define('userBlogs', {
        id: {
            type: DataTypes.INTEGER(10),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        email: {
            type: DataTypes.STRING(255),
            allowNull: false
        },
        blogdetails: {
            type: DataTypes.TEXT,
            allowNull: false
        },
        tags: {
            type: DataTypes.STRING(255),
            allowNull: false
        },
    }, {
        timestamps: true,
        tableName: 'userBlogs'
    });

    return userBlogs;
};

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading