Multipart form data file uplaod time comming error body should be object, and i am using ajv plugin also, still i am using same issue. below is my reference code.
app.js
const fastify = require('fastify')({
logger: true
});
const Ajv = require('ajv');
const ajv = new Ajv({
useDefaults: true,
coerceTypes: true,
$data: true,
extendRefs: true
});
ajv.addKeyword("isFileType", {
compile: (schema, parent, it) => {
parent.type = "file";
delete parent.isFileType;
return () => true;
},
});
fastify.setSchemaCompiler((schema) => ajv.compile(schema));
routes.js
schema: {
tags: [{
name: 'Category'
}],
description: 'Post category data',
consumes: ['multipart/form-data'],
body: {
type: 'object',
isFileType: true,
properties: {
name: {
type: 'string'
},
thumb_url: {
isFileType: true,
type: 'object'
},
img_url: {
isFileType: true,
type: 'object'
},
status: {
type: 'number',
enum: [0, 1],
default: 1
}
},
required: ['name', 'thumb_url', 'img_url']
},
response: {
201: {
type: 'object',
properties: categoryProperties
}
}
}
response
{
"statusCode": 400,
"error": "Bad Request",
"message": "body should be object"
}
I suspect the error is in the way I send the data, but I am having trouble figuring it out. I have read about this error and it seems to be generated when an object is passed to formData, but I am sending a string so I don't understand why it happens. thanks in advance!