无法读取未定义错误的属性“替换”-使用multer上传图片

时间:2019-05-14 09:15:50

标签: javascript node.js express multer

当我尝试将图像和一些文本发布到数据库时出现此错误。这是显示表单外观的图像:http://prntscr.com/nob9r3

这是完整的错误:

TypeError: Cannot read property 'replace' of undefined
at E:\WEB\WebProjekat\routes\admin_products.js:79:22
at Layer.handle [as handle_request] 
(E:\WEB\WebProjekat\node_modules\express\lib\router\layer.js:95:5)
at next 
(E:\WEB\WebProjekat\node_modules\express\lib\router\route.js:137:13)
at Immediate.<anonymous> (E:\WEB\WebProjekat\node_modules\multer\lib\make- 
middleware.js:53:37)
at runCallback (timers.js:706:11)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)

我在网站上建立了这样的解释:“问题在于,multer首先保存文件,然后才写入请求表单的其余部分(例如隐藏字段)。”

但是我真的不知道这是否是问题,如果是那我不知道如何解决它,因为im是NodeJS中的新功能。

我正确包含了multer

var multer = require('multer');

var storage = multer.diskStorage({
destination: function (request, file, callback) {
    callback(null, '.public/slike_proizvoda');
},
filename: function (request, file, callback) {
    console.log(file);
    callback(null, file.originalname)
}
});
var upload = multer({storage: storage});

这是我的router.post代码的一部分:

router.post('/dodaj-proizvod', upload.single('image'), function (req, res) {

var imageFile = typeof req.file !== "undefined" ? req.file.filename : "";

req.checkBody('title', 'Morate uneti naziv proizvoda.').notEmpty();
req.checkBody('desc', 'Morate uneti opis proizvoda.').notEmpty();
req.checkBody('price', 'Morate uneti cenu proizvoda.').isDecimal();
req.checkBody('image', 'Morate uploadovati sliku proizvoda').isImage(imageFile);

var title = req.body.title;
var slug = title.replace(/\s+/g, '-').toLowerCase();
var desc = req.body.desc;
var price = req.body.price;
var category = req.body.category;

var errors = req.validationErrors();

if (errors) {
    Category.find(function (err, categories) {
        res.render('admin/dodaj_proizvod', {
            errors: errors,
            title: title,
            desc: desc,
            categories: categories,
            price: price
        });
    });
} else {
    Product.findOne({slug: slug}, function (err, product) {
        if (product) {
            req.flash('danger', 'Uneli ste naslov proizvoda koji vec postoji, izaberite drugi.');
            Category.find(function (err, categories) {
                res.render('admin/dodaj_proizvod', {
                    title: title,
                    desc: desc,
                    categories: categories,
                    price: price
                });
            });
        } else {
            var price2 = parseFloat(price).toFixed(2);
            var product = new Product({
                title: title,
                slug: slug,
                desc: desc,
                price: price2,
                category: category,
                image: imageFile
            });

            product.save(function (err) {
                if (err)
                    return console.log(err);

                mkdirp('public/slike_proizvoda/' + product._id, function (err) {
                    return console.log(err);
                });

                mkdirp('public/slike_proizvoda/' + product._id + '/galerija', function (err) {
                    return console.log(err);
                });

                mkdirp('public/slike_proizvoda/' + product._id + '/galerija/ikonice', function (err) {
                    return console.log(err);
                });

                if (imageFile != "") {
                    var productImage = req.file;
                    var path = 'public/slike_proizvoda/' + product._id + '/' + imageFile;

                    productImage.mv(path, function (err) {
                        return console.log(err);
                    });
                }

                req.flash('success', 'Proizvod je dodat!');
                res.redirect('/admin/proizvodi');
            });
        }
    });
}

});

0 个答案:

没有答案