将Multer与Bodyparser一起使用

时间:2020-05-12 18:57:05

标签: node.js express postman multer body-parser

router.post('/add',upload.single('productImage'), (req, res, next) => {

let newProduct = new Product({
    productName: req.body.productName,
    unitPrice: req.body.unitPrice,
    minimumOrder: req.body.minimumOrder,
    category: req.body.category,
    availability: req.body.availability,
    imgSrc : req.file.path
});

newProduct.save()
    .then(() => {
        res.status(201).json({
            success: 'true',
            msg: 'Product Added Success.!'
        });
    })
    .catch((err) => {
        res.status(400).json({
            success: 'false',
            msg: 'Product Added Failed .!'
        });
    });

});

这是我编写的代码,用于上传文件并将其存储在我的图像文件夹中,同时将其他json数据发送到数据库。我在和邮递员测试。事实证明,该文件没有任何问题,存储在所需的文件夹中,并且从multer的角度来看没有错误。但是我无法将其他数据发送到数据库。所以我想知道这怎么会发生。我在邮递员中使用表单数据主体发送数据。

0 个答案:

没有答案