Router.post два определения мульти в одном почтовом запросе - PullRequest
0 голосов
/ 03 марта 2020

У меня есть два разных файла ввода (изображения), которые должны быть сохранены в 2 разных папках, поэтому я создал две функции мультитера и хочу реализовать их в одном пост-запросе, как я могу это сделать? Фактический способ, которым я делаю это, выдает ошибку : paintingUploadPath.any не является функцией

маршрутизатор. js

const imageMimeTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/jpg'];
const includeMimeTypes = (req, file, callback) => {
    callback(null, imageMimeTypes.includes(file.mimetype))
}

const profilpicUpload = multer({
    dest: profilpicUploadPath,
    fileFilter: includeMimeTypes
});
const paintingspicUpload = multer({
    dest: paintingsUploadPath,
    fileFilter: includeMimeTypes
});


router.get('/create', (req, res) => {
    res.render('admin/createPost/index', {
        layout: 'layouts/admin',
        // Defined in models/post, gets variables
        post: new Post()
    });
})
// New Post create
router.post('/', profilpicUpload.single('profilpicture'), paintingsUploadPath.any(), async (req, res) => {
    // profilpicture
    const fileName = req.file != null ? req.file.filename : null;
    const post = new Post({
        // get information from sended new Post (above) and defines variables to use in ejs files
        surname: req.body.surname,
        name: req.body.name,
        bio: req.body.bio,
        profilpic: fileName,
        paintings: fileName
    })
    try {   // await for new post to safe
        const newPost = await post.save();
        res.redirect('admin');
    } catch {
        if (post.profilpic != null)
        removeProfilpic(post.profilpic)
        renderNewPage(res, post, true)
    }
})


function removeProfilpic(fileName) {
    fs.unlink(path.join(profilpicUploadPath, fileName), err => {
        if (err) console.log(err)
    })
}

async function renderNewPage(res, post, hasError = false) {
    // Implement all Posts (artists) in the DB
    try {
        const posts = await Post.find({}).collation({ locale: 'en', strength: 2 }).sort({ name: 1 })  // wait and find all post and sort my name
        const params = {
            layout: 'layouts/admin',
            posts: posts,    // take all posts from await Post.find({}) and overrides the updates the posts
        }
        if (hasError) params.errorMessage = 'Ein Fehler ist aufgetreten'
        res.render('admin/index', params);
    } catch (err) {
        res.redirect('/');
    }
}

Я установил express e js multer, ... no multigrid!

...