无法使用sequelize.js将数据保存到PostgreSQL数据库,但系统返回列多次。

huangapple go评论60阅读模式
英文:

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

问题

在尝试使用Sequelize的BlogModel.create()系统将数据保存到postgres数据库时,系统未能将数据保存到表中,并且服务器在控制台中多次返回createdatupdatedat列(请参见下文)。在模式中,我只添加了一次该列,有人能就此问题提供建议吗?

执行(默认):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

'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:'无效的博客请求或博客请求为空!');
    } 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);
}

});

英文:

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

'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);
   }
});

答案1

得分: 1

createdAtupdatedAt 多次出现是因为您已添加了列 (createdAt 和 updatedAt),同时设置了 timestamps:true,timestamps 也会添加这两列,可以使用列或 timestamps。

英文:

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;
};

huangapple
  • 本文由 发表于 2023年2月16日 14:29:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定