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

req.body is empty #10

Open
idoor88 opened this issue Sep 13, 2021 · 1 comment
Open

req.body is empty #10

idoor88 opened this issue Sep 13, 2021 · 1 comment

Comments

@idoor88
Copy link

idoor88 commented Sep 13, 2021

Hi I tried this code below with values for both caption and filename:

`
let formData = new FormData();

formData.append('caption', caption);

formData.append('file', {
name: filename
});

console.log('upload File formData: ', formData);

axios.post('http://localhost:9890/', formData)
.then((response) => {
response.data.success ? alert('File successfully uploaded') : alert('File already exists');
this.fetchRecent();
})
.catch(err => alert('Error: ' + err));
`

When I uploaded an image file, on the server side, the logging showed the req.body is empty and req.file is undefined:

Upload single file req.body: {}
Upload single file req.file: undefined

The server side code:

`
imageRouter.route('/')

    .post(upload.single('file'), (req, res, next) => {

        console.log(req.body);

        // check for existing images
        Image.findOne({ caption: req.body.caption })

            .then((image) => {
                console.log(image);
                if (image) {
                    return res.status(200).json({
                        success: false,
                        message: 'Image already exists',
                    });
                } 

`
...
Do you have an idea what I did wrong?
Thanks

@sadath-12
Copy link

because in

formData.append('file', {
name: filename
});

you just gave name try to even give value

append(name, value)

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

2 participants