0

This seems like a simple problem, but I've been spending a while trying to solve it and I can't quite figure out what's wrong. This is my frontend form that is making the request:

div#PreGA
    p PreGa.json:
    form(action="config/set/PreGa", name="pre-ga", method="post", enctype="multipart/form-data")
        input(type="file" value="Choose File" accept=".json")#choose-file-pre-ga
        input(type="submit" value="Upload")#upload-pre-ga

This is the route that should receive the request:

var upload = multer({ dest: 'uploads/' });

//set the JSON file for the pre-ga reported issues
router.post('/config/set/PreGa',ensureAuthenticated, upload.single('pre-ga'), function(req, res, next) {


    console.log(req.body);
    console.log(req.file);

});

The issue is that req.file, which should return the file, is undefined when the route is called.

This is one of the simplest use cases, and I can't figure out what's going wrong. It would greatly appreciate some help.

1 Answer 1

1

Your file input is missing a name attribute.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I thought that I needed the name for the form input... Thanks. Also, it should be req.file and not req.files because I'm using single() and not array() according to the multer documentation. It does work now, though. I can't believe I didn't see that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.