A
A
Alexey Yakovlev2021-04-25 19:34:36
Audio
Alexey Yakovlev, 2021-04-25 19:34:36

Error downloading music?

Error:
MulterError: Unexpected field
at wrappedFileFilter (C:\Users\alexe\Desktop\music-platform\node_modules\multer\index.js:40:19)
at Busboy. (C:\Users\alexe\Desktop\music-platform\node_modules\multer\lib\make-middleware.js:114:7)
at Busboy.emit (events.js:315:20)


Form in pug:

form(action="/profile/"+user._id+"/add-music" method="POST" enctype="multipart/form-data")
        label(for="file-music") Load new music
        input#file-music(type="file" name="music" style="display: none;" required)
        input(name="name" type="text" placeholder="Name" required)
        input(type="submit" value="Load")


server.js middlewares:
app.use(flash());
app.use(varsMiddleware);
app.use(userMiddleware);
app.use(fileMiddleware.single('photo'));
app.use(musicFileMiddleware.single('music'));


file-music.js:
const multer = require('multer');

const storage = multer.diskStorage({
    destination(req, file, cb) {
        cb(null, 'user-music');
    },
    filename(req, file, cb) {
        cb(null, new Date().toISOString().replace(/:/g, '-') + '-' + file.originalname);
    }
})

const allowedTypes = ['audio/mp3'];

const fileFilter = (req, file, cb) => {
    if (allowedTypes.includes(file.mimetype)) {
        cb(null, true);
    } else {
        cb(null, false);
    }
}

module.exports = multer({storage, fileFilter});


profile router:
router.post('/:id/add-music', async(req, res) => {
    try {
        const fileMusic = req.file.path.replace('user-music', '').replace(/[\\\/]/, '');
        const {name} = req.body;
        const findUser = await User.findById(req.params.id);

        await User.updateOne({
            email: findUser.email
        }, {$push: {
            music: {
                audio: {$each: [{name, fileMusic, favorite: false}], $position: 0}
            }
        }})
    } catch (e) {
        console.log(e.message);
    }
})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question