Задать вопрос
@aleshaykovlev
html, css, js, node, webpack, sass, react

Ошибка при загрузке музыки?

Ошибка:
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)


Форма в 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);
    }
})
  • Вопрос задан
  • 80 просмотров
Подписаться 1 Средний Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы