Multer unable to process files

19 Views Asked by At

I am making a serverless function using node. req.file always appears to be undefined for my image upload function.

I have tried testing the API both with Postman and by making an HTML form for uploading the file.

This is my auth_routes.ts file where I define Multer and the route for the upload endpoint:

const router = express.Router();
const upload = multer({ storage: multer.memoryStorage() })
router.route('/registerUser').post(register);
router.post("/profilePicture/upload", upload.single('image'), uploadProfilePicture)
router.route('/registerBusiness').post(registerBusiness)

export default router

This is the function where I check if the upload file is undefined or not:

export const uploadProfilePicture = asyncHandler(async (req, res) => {
    logger.info("in the service function")
    logger.info(req.body)
    const regRepo = registrationRepository(db, storage);
    if(req.file === undefined){
        res.status(StatusCode.BAD_REQUEST).json({ message: "file not present" });
        logger.error("file not present")
        throw new UploadError("file not present")
    }

    const uid:string = req.body.uid;
    try {
        logger.info("uploading image")
        const url = await regRepo.uploadProfilePicture(req.file, uid); 
        res.status(StatusCode.OK).json({ message: "upload successful", url })
    } catch (error) {
        logger.error("Error uploading image", error);
        res.status(StatusCode.INTERNAL_SERVER_ERROR).json({ message: error });
        throw new UploadError("Error uploading image")    
    }
})

What could be causing the req.file to be undefined?

0

There are 0 best solutions below