richardgirges / express-fileupload

Simple express file upload middleware that wraps around busboy
MIT License
1.52k stars 261 forks source link

mv is not a function error #308

Closed KuroChu closed 2 years ago

KuroChu commented 2 years ago

So I'm getting an error that mv is not an function but only when it gets one file but if there more than one it works just fine Here's the code if it helps:

app.post('/upload-photos', async (req, res) => {
    var id = 0;
    try {
        if(!req.files) {
            res.send({
                status: false,
                message: 'No file uploaded'
            });
        }else{
            let data = [];

            //loop all files
            _.forEach(_.keysIn(req.files.photos), (key) => {
                let photo = req.files.photos[key];

                //move photo to uploads directory
                try {
                    photo.mv('./uploads/' + photo.name);
                }catch(e) {
                    console.log(e);
                }

                //push file details
                data.push({
                    name: photo.name,
                    mimetype: photo.mimetype,
                    size: photo.size
                });
            });

            //return response
            res.send({
                status: true,
                message: 'Files are uploaded',
                data: data
            });
            //console.log(data);
        }
    } catch (err) {
        res.status(500).send(err);
    }
});