[英]{"message": "notNull Violation: users.firstName cannot be null,\nnotNull Violation: users.lastName can not be null} error I am facing
當用戶注冊時,我讓用戶 model 字段允許 null 為真,然后再次登錄用戶可以上傳他們的個人資料圖片,但是當我測試此代碼時,我遇到錯誤 {"message": "notNull Violation: users.firstName cannot為 null,\nnotNull 違規:users.lastName 不能為 null}???
我現在應該怎么辦
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;
```
在users.create
,您應該傳遞所有必填字段,或者在您只想更新圖像的情況下使用更新(這似乎是您在上面的代碼中想要做的)。
此外,如果第二種情況是您想要的,則此操作應定義為 PUT/PATCH 而不是 POST。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.