I have this simple upload form, I'm using postman to send the file
the first 4 times I post the same file, everything works as expected but on the 5th time 'data.fields.username' is undefined, even though nothing has changed between requests.
The exact error I get is
{
"error": "Internal Server Error",
"statusCode": 500,
"message": "Cannot read properties of undefined (reading 'value')"
}
when I log out data.fields on the post that fails I get this
and If I log data.fields.username i get undefined
And the code I'm using...
async function uploadFile(fastify: any, _options: Object) {
fastify.register(require("@fastify/multipart"));
fastify.post(
"/v1/uploads/uploadFile",
postOpts,
async function (request: any, reply: any) {
const data = await request.file();
console.log("===============================================");
console.log(data.fields.username.value); // <=== on the 5th / 6th post this fails
console.log("===============================================");
const statusCode: number = 201;
reply
.code(statusCode)
.header("Content-Type", "application/json; charset=utf-8")
.send({ statusCode });
}
);
}
const postOpts = {
schema: {
summary: "Upload File",
consumes: ["multipart/form-data"],
// body: {
// type: "object",
// properties: {
// username: {
// type: "string",
// },
// },
// required: ["username"],
// },
response: {
200: {
type: "object",
description:
"The request was successful. The response will contain a JSON body.",
properties: {
uploadConfirmed: { type: "boolean" },
upoadConfirmedTime: { type: "number" },
},
},
400: {
type: "object",
description: "The request was invalid and/or malformed.",
properties: {
error: { type: "string" },
statusCode: { type: "number" },
message: { type: "string" },
},
},
500: {
type: "object",
description: "There was an internal error.",
properties: {
error: { type: "string" },
statusCode: { type: "number" },
message: { type: "string" },
},
},
},
} as const,
};
export { uploadFile };
my tsconfig is
{
"compilerOptions": {
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"outDir": "./build" /* Redirect output structure to the directory. */,
"strict": true /* Enable all strict type-checking options. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}
I'm running node version v16.14.0
on a mac