【问题标题】:Problem uploading multiple images in node.js/Mongodb with Multer and Cloudinary使用 Multer 和 Cloudinary 在 node.js/Mongodb 中上传多个图像时出现问题
【发布时间】:2021-01-24 20:17:54
【问题描述】:

我在尝试将多张图片上传到我的页面时遇到问题。我正在使用 Multer 和 Cloudinary。由于 Cloudinary 没有用于多次上传的方法,因此我通过 forEach 传递了图像数组(之前使用 Multer 提取)。好的,图片已成功上传到mi cloudinary容器。当我尝试将安全 url 和公共 id 推送到 req.body.product 时,问题出现在上传功能中。如果我在上传函数之外使用 console.log(req.body.product),它会返回名称和价格(来自表单的数据),但不会返回图像数组和 id。但是,如果我在 de upload 函数中对其进行 console.log,它会返回完整的对象(名称、价格和两个数组)。 Product.create 正在获取没有数组的对象,因此我将图像上传到 Cloudinary,但我无法将 url 推送到我的数据库中以在展示页面中使用。在我看来,我忽略了一个范围问题,但我无法弄清楚。 Multer 和 cloudinary 配置正确,我可以在其他路线上上传单个图像。

提前致谢

// CREATE PRODUCT
router.post('/categorias/:categorie', middleware.adminCheck, upload.array('images'), function (req, res) {
    Categorie.findOne({url: req.params.categorie}, function (err, categorie) {
        if (err) {
            console.log(err);
        } else {
            req.body.product.images = [];
            req.body.product.ids    = [];
            req.files.forEach(function (file) {
                cloudinary.uploader.upload(file.path, function (result) {
                    req.body.product.images.push(result.secure_url);
                    req.body.product.ids.push(result.public_id);
                    console.log(req.body.product);
                });
            });
            console.log(req.body.product);
            Product.create(req.body.product, function (err, product) {
                if (err) {
                    console.log(err);
                } else {
                    categorie.products.push(product);
                    categorie.save();
                    res.redirect('/categorias/' + categorie.url);
                }
            });     
        }
    });
});

[-----更新-----]

我使用 promises 甚至 setTimeout 修复了时间问题,但我发现问题仍然存在。至少在这两种情况下,如果我在 Product.create 之前 console.log(req.body.product) 得到完整的对象,如下所示:

[Object: null prototype] {
  name: '12',
  price: '12',
  images:
   [ 'https://res.cloudinary.com/dm3wxeaiz/image/upload/v1602339704/xjknwth8mscexukjaznv.jpg',
     'https://res.cloudinary.com/dm3wxeaiz/image/upload/v1602339704/pddcxhvxfcc2zjfjjk3c.jpg' ],
  ids: [ 'xjknwth8mscexukjaznv', 'pddcxhvxfcc2zjfjjk3c' ] }

但是,如果在 Product.create 中的任何地方使用控制台记录产品,我会得到:

{ _id: 5f81c37fbf3da50fb6ba3e1c, name: '12', price: 12, __v: 0 }

最终存储在我的数据库中。 它与我在 Product.create 之前得到的 [Object: null prototype] 有关系吗?出于某种原因,似乎没有办法通过 Product.create 传递完整的对象。会不会是正文解析器的问题?

【问题讨论】:

    标签: node.js mongodb mongoose multer cloudinary


    【解决方案1】:

    Cloudinary 上传器是异步的,当您在上传器范围之外打印 console.log 时,它可能尚未完成执行,但它最终会在上传器内部执行并返回结果。下面是一个使用 Promise 和 then() 调用实现它的示例:Cloudinary api - resolve promise

    【讨论】:

      【解决方案2】:

      您可以通过以下方式重新排列代码,以便 Cloudinary 响应数据可用于 Product.create 函数:

      router.post(
        '/categorias/:categorie',
        middleware.adminCheck,
        upload.array('images'),
        function(req, res) {
          Categorie.findOne({ url: req.params.categorie }, function(err, categorie) {
            if (err) {
              console.log(err)
            } else {
              req.body.product.images = []
              req.body.product.ids = []
              req.files.forEach(function(file) {
                cloudinary.uploader
                  .upload(file.path)
                  .then(result => {
                    req.body.product.images.push(result.secure_url)
                    req.body.product.ids.push(result.public_id)
                    console.log(req.body.product)
                    Product.create(req.body.product, function(err, product) {
                      if (err) {
                        console.log(err)
                      } else {
                        categorie.products.push(product)
                        categorie.save()
                        res.redirect('/categorias/' + categorie.url)
                      }
                    })
                  })
                  .catch(error => {
                    console.log(error)
                  })
              })
            }
          })
        }
      )
      
      

      【讨论】:

        猜你喜欢
        • 2019-02-21
        • 2020-12-25
        • 2021-06-10
        • 2017-05-22
        • 2020-04-07
        • 2023-04-11
        • 2020-10-26
        • 2019-02-13
        • 1970-01-01
        相关资源
        最近更新 更多