I am attempting to create POST requests to upload images from a mobile app to a digital ocean space (S3 compatible)
Environment Information
koa-body: 4.2.0
Koa: 2.13.1
Current Behavior
The request is successful and I am able to see the result in my server logs (see screenshot). However, I need to attach the file name (string) and file contents (binary) as part of the parameters required to upload to the space bucket. When trying to access these properties in my route/post definition in typescript, I am getting errors of their being undefined. This occurs with any syntax attempted in the code included in Steps to Reproduce.
Steps to Reproduce
Here is the code to handle post requests from the mobile app. Refer to where I mark //error occurs here. I receive the pop up message that files is of type "File | Undefined" and the warning that the "Object is possibly undefined."
import { authenticateJWT, checkAuthenticated } from 'server/auth';
import { Context, DefaultState } from 'koa';
import Koa from 'koa';
import mount from 'koa-mount';
import Router from 'koa-router';
import { addUploadedFile } from 'server/external/file_upload';
import koaBody from 'koa-body';
// setup router for authenticated API routes
const apiRouter = new Router<DefaultState, Context>();
// allow JWT usage on api routes for auth
apiRouter.use(authenticateJWT);
// always check that the user is authenticated before all routes
apiRouter.use(checkAuthenticated);
apiRouter.post('/file-upload', koaBody({ multipart: true }), async (ctx) => {
const type = ctx.request.type;
console.log(`type: ${type}`);
const user = ctx.state.user;
console.log('user: ', JSON.stringify(user, null, 2));
const files = ctx.request.files;
console.log(`files: `, JSON.stringify(files, null, 2));
const { fileName, fileContents } = ctx.request.files.testimage // error occurs here
try {
const fileId = addUploadedFile(fileName, fileContents, user);
if (fileId) {
ctx.status = 200;
ctx.body = {
status: 'success',
};
} else ctx.status = 400;
} catch {
ctx.status = 400;
}
});
const app = new Koa();
app.use(apiRouter.routes()).use(apiRouter.allowedMethods());
export const apiApp = mount('/api/1', app);
Additional comments
Note that the solution offered in issue #171 does not seem to work for me
Problem/Feature Request Summary
I am attempting to create POST requests to upload images from a mobile app to a digital ocean space (S3 compatible)
Environment Information
Current Behavior
The request is successful and I am able to see the result in my server logs (see screenshot). However, I need to attach the file name (string) and file contents (binary) as part of the parameters required to upload to the space bucket. When trying to access these properties in my route/post definition in typescript, I am getting errors of their being undefined. This occurs with any syntax attempted in the code included in Steps to Reproduce.
Steps to Reproduce
Here is the code to handle post requests from the mobile app. Refer to where I mark //error occurs here. I receive the pop up message that files is of type "File | Undefined" and the warning that the "Object is possibly undefined."
Additional comments
Note that the solution offered in issue #171 does not seem to work for me