A
A
Andrey2017-07-29 12:49:33
Node.js
Andrey, 2017-07-29 12:49:33

Why doesn't moddlewear for Express Multer save the image?

In the form (written in ReactJS) I form the fields for sending the request:

const formData = new FormData();
        formData.append('name', this.state.name);
        formData.append('quantity', this.state.quantity);
        formData.append('code', this.state.code);
        formData.append('images', this.images);

       addSku(formData);

Then I send it to the server:
export function addSku(sku) {
    console.log('sku', sku.get('images')); // sku [object File]
    return new Promise((resolve, reject) => {
        fetch('/api/sku', {
            method: 'POST',
            credentials: 'same-origin',
            body: sku
        })
            .then(res => res.json(res))
            .then(json => resolve(json))
            .catch(err => reject(err));
    });
}

On the server I catch the field:
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });

app.post('/', upload.single('images'), (req, res) => {
    console.log('req.file', req.file); // undefined
    console.log('req.files', req.files); // undefined
    console.log('req.body', req.body); // { name: '', quantity: '0', code: '', images: '[object File]' }
console.log(req.headers['content-type']); // multipart/form-data; boundary=----WebKitFormBoundaryFwa3zhYAN8cJcU5g
 });

According to the documentation , the file or files field should appear in req. They are not here. The uploads/ directory is created.
There are no errors, but the file does not appear in the directory.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2017-07-29
@f-end

Question removed.
It turned out that upload.single does not take one first file from the array (as I assumed), but works in one object of the File type

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question