I have tried to create a device using Device.create({...})
, but see the following error:
Executing (default): INSERT INTO "devices" ("id","name","price","rating","img","createdAt","updatedAt","BrandId","TypeId") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7,$8) RETURNING "id","name","price","rating","img","createdAt","updatedAt","BrandId","TypeId";
Error: SequelizeUniqueConstraintError: Validation error
at Function.internal (file:///.../react+node.js/server/build/api/error/ApiError.js:14:16)
at file:///.../react+node.js/server/build/api/controllers/deviceController.js:52:31
at Generator.throw (<anonymous>)
at rejected (file:///.../react+node.js/server/build/api/controllers/deviceController.js:5:65)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Definition of User:
// this I use just for typing but added just in case
export class Device extends Model<InferAttributes<Device>, InferCreationAttributes<Device>> {
declare id: CreationOptional<number>
declare name: string
declare price: number
declare rating: CreationOptional<number>
declare img: string
declare TypeId: ForeignKey<Type['id']>
declare BrandId: ForeignKey<Brand['id']>
declare createdAt: CreationOptional<Date>
declare updatedAt: CreationOptional<Date>
}
Device.init(
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: {
type: DataTypes.STRING,
unique: true,
allowNull: false
},
price: {
type: DataTypes.INTEGER,
allowNull: false
},
rating: {
type: DataTypes.INTEGER,
defaultValue: 0
},
img: {
type: DataTypes.STRING,
allowNull: false
},
createdAt: DataTypes.DATE,
updatedAt: DataTypes.DATE
},
{
sequelize,
tableName: 'devices'
}
)
Device.hasMany(Rating)
Rating.belongsTo(Device)
Device.hasMany(BasketDevice)
BasketDevice.belongsTo(Device)
Device.hasMany(DeviceInfo, { as: 'info' })
DeviceInfo.belongsTo(Device)
But:
- I could create devices before
- I can still create other types of data(that do not need to upload an image)
- I used the debugger to see all data types that go as parameters to
create
and saw that they were correct.
My createDevice
function
async (req: deviceRequestCreate, res: Response, next: NextFunction): Promise<void> {
const { BrandId, TypeId, info, name, price } = req.body
if (!BrandId) return next(ApiError.internal('BrandId is required'))
if (!TypeId) return next(ApiError.internal('TypeId is required'))
if (!name) return next(ApiError.internal('Name is required'))
if (!price) return next(ApiError.internal('Price is required'))
const numberBrandId = parseInt(BrandId)
const numberTypeId = parseInt(TypeId)
const numberPrice = parseInt(price)
if (!req.files) return next(ApiError.internal('Image is not uploaded'))
const { img } = req.files
if (Array.isArray(img)) {
return next(ApiError.internal('Uploading more than one image is not supported yet'))
}
const fileName = uuidv4() + '.jpg'
img.mv(path.resolve('src/static/' + fileName))
// HERE ERROR OCCURRED
const newDevice = await Device.create({ name, price: numberPrice, TypeId: numberTypeId, BrandId: numberBrandId, img: fileName })
res.json(newDevice)
}
I make post requests through the Insomnia:
I have
Content-Type
in the header for multipart/form-data
and authorization token.
From what I understand, it is a problem with types. But I do not know where.
What should I do?