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}???
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).
Also, if the second case is what you want, this action should be defined as PUT/PATCH and not POST.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.