[英]{"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.