简体   繁体   English

{"message": "notNull Violation: users.firstName cannot be null,\nnotNull Violation: users.lastName cannot be null} 我面临的错误

[英]{"message": "notNull Violation: users.firstName cannot be null,\nnotNull Violation: users.lastName can not be null} error I am facing

when user signup I have keep the user model field allow null to true and after that on again login user can upload their profile pic but when I test this code I am facing the error {"message": "notNull Violation: users.firstName cannot be null,\nnotNull Violation: users.lastName can not be null}???当用户注册时,我让用户 model 字段允许 null 为真,然后再次登录用户可以上传他们的个人资料图片,但是当我测试此代码时,我遇到错误 {"message": "notNull Violation: users.firstName cannot为 null,\nnotNull 违规:users.lastName 不能为 null}???

what should I do now我现在应该怎么办

const Storage = multer.diskStorage({
  destination: (req, file, cb) => {
      cb(null, './public/images')
  },

  filename: (req, file, callBack) => {
    callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
},

});

const multerFilter = (req, file, cb) => {
    var ext = path.extname(file.originalname);
    if(ext !== '.png' && ext !== '.jpg' && ext !== '.gif' && ext !== '.jpeg') {
      cb(new Error('Only images are allowed'))
    }
    cb(null, true)
};
const updateFile = multer({
  storage: Storage,
  limits: 3 * 1024 * 1024, //max 3MB file upload
  fileFilter: multerFilter,
});




router.post("/profile" , updateFile.single("upload"), async(req, res)=>{
  console.log('hello')
  console.log(req.file)
  if (req.file == undefined) {
    return res.status(400).json({ message:'please select the file' });
  }
  console.log('inside the db')
  try {
    await users.create({
   image: req.file.filename
  })
  .then(msg=>{
      res.status(200).json({ message: "profile is uploaded successfully!" });
  });
  console.log('outside the db')
  } catch (error) {
    res.status(500).json({ message: error.message });
  }      
});

this is my user model and I am using sequelize


```
import { Sequelize } from "sequelize";
import db from "../config/Database.js";

const Datatypes = Sequelize;

const users  = db.define('users', {
    uuid:{
        type: DataTypes.STRING,
        defaultValue: DataTypes.UUIDV4,
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    firstName: {
        type: DataTypes.STRING,
        allowNull: false,
      
    },
    lastName:{
        type: DataTypes.STRING,
        allowNull: false,
        
    },
    email: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true,
        validate: {
            notEmpty: true,
            isEmail: true,
        },
    },
    password: {
        type: DataTypes.STRING,
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    image: {
        type: DataTypes.STRING,
        allowNull: true
    },
    role:{
        type: DataTypes.STRING,
        allowNull: false,
        validate:{
            notEmpty: true
        }
    }
}, {
    freezeTableName: true
})

export default users;
```

On the users.create , you should either pass all required fields, or use update in case you only want to update the image (which seems like what you wanted to do in the code above).users.create ,您应该传递所有必填字段,或者在您只想更新图像的情况下使用更新(这似乎是您在上面的代码中想要做的)。

Also, if the second case is what you want, this action should be defined as PUT/PATCH and not POST.此外,如果第二种情况是您想要的,则此操作应定义为 PUT/PATCH 而不是 POST。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 not null 违例:model.field cannot be null - not null violation: model.field cannot be null 设置Flatlist numColumn> = 2时,我遇到问题,引发违规错误 - I am facing an issue while setting Flatlist numColumn >=2, throwing a violation error 如何将 firstName 和 lastName 添加到 fullName? - How can I add firstName and lastName to fullName? Object 与用户,当 2 个值匹配时删除除一个以外的所有值(名字,姓氏) - Object with users, remove all but one when 2 values match (firstname, lastname) 运行测试时出现“不变违规:本机模块不能为空”。 - Getting ''Invariant Violation: Native module cannot be null.'' when I run the test 不变违规:Native Module 不能是 Null。 错误仅出现在 iOS - Invariant Violation: Native Module cannot be Null. Error only showing up on iOS 我如何在反应中将名字/姓氏更改为名字/姓氏? 问题是它来自后端 - how can i change firstname/lastname to firstName/lastName in react? the problem is that it comes from the backend 为什么我收到此错误消息:(空属性) - Why am i getting this error message am getting: (property of null) 渲染时无法读取null的属性“ firstName” - Cannot read property 'firstName' of null at render 无法在 Reactjs 中读取 null 的属性“firstname” - Cannot read property 'firstname' of null in Reactjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM