Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing form fields before a custom storage engine #1188

Open
rzmay opened this issue Feb 22, 2023 · 0 comments
Open

Accessing form fields before a custom storage engine #1188

rzmay opened this issue Feb 22, 2023 · 0 comments

Comments

@rzmay
Copy link

rzmay commented Feb 22, 2023

I'm currently using Multer with a custom storage engine that uploads a file to a location specified in the request. I'm currently getting the destination and a couple other parameters from the query, but I'd prefer to consolidate all input data into the form data. This destination input requires some extra validation that needs access to res and next. The current implementation is as follows:

upload(req, res, next) {
    // Ideally this would be `req.body.destination`;
    const destination = req.query.destination;
    
    /* Validation ... */

    return multer({
        storage: new CustomStorage(destination),
    }).single('file')(req, res, next);
}

However, Multer needs to run before this validation in order to parse the form data into req.body in the first place. I attempted to solve this by running a second Multer instance before the storage engine -- this instance would ignore the files and parse any text fields into req.body. The implementation was as follows:

multer({
    fileFilter: (req, file, cb) => { return cb(null, false); },
}).any();

Running this did allow me to access the form data in req.body in the upload middleware, but then I received the following error:

Error: Unexpected end of form
    at Multipart._final (/Users/robert/bucket/node_modules/busboy/lib/types/multipart.js:588:17)
    at callFinal (node:internal/streams/writable:694:27)
    at prefinish (node:internal/streams/writable:723:7)
    at finishMaybe (node:internal/streams/writable:733:5)
    at Multipart.Writable.end (node:internal/streams/writable:631:5)
    at onend (node:internal/streams/readable:693:10)
    at processTicksAndRejections (node:internal/process/task_queues:78:11) {
  storageErrors: []
}

I attempted to downgrade to Multer 1.4.3 after reading issue 1144, and doing so did stop the error, but resulted in the API route erroring out with a 400 status and no error message instead.

How can I access the form data fields here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant