gridfs-file-storage icon indicating copy to clipboard operation
gridfs-file-storage copied to clipboard

req.body is empty

Open idoor88 opened this issue 3 years ago • 1 comments

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

idoor88 avatar Sep 13 '21 21:09 idoor88

because in

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

you just gave name try to even give value

append(name, value)

sadath-12 avatar Nov 29 '22 17:11 sadath-12