gridfs-file-storage
gridfs-file-storage copied to clipboard
req.body is empty
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
because in
formData.append('file', {
name: filename
});
you just gave name try to even give value
append(name, value)