【问题标题】:Multiple Uploads in cloudinary using Node Js and ForEach loop使用 Node Js 和 ForEach 循环在 cloudinary 中多次上传
【发布时间】:2020-07-13 16:25:27
【问题描述】:

我有一个输入标签来上传多张图片,并使用 cloudinary 和强大的包将它们上传到 cloudinary。

理想情况下,我想提取 URL 并将其保存在我拥有所有其余信息的数据库中。

但未能检索到数组中显示的以下 URL。

以它为目标创建一个对象,并使用 URLs 保存在数据库中。


app.post("/newcampbook",(req,res)=>{
  new formidable.IncomingForm({ multiples: true }).parse(req,(err,fields,file)=>{
   if(!err){     
     var pathArray = [];
        file.image.forEach(e =>{
            pathArray.push(e.path)
        })
        var savedUrl = [];
        pathArray.forEach(e =>{
            cloudinary.uploader.upload(e, (err,savedImage)=>{
                if(!err){
                    savedUrl.push(savedImage.url)
                    console.log(savedUrl)
                }
            })
        })
    res.send("Testing to upload")
   } else {
    console.log(err);
    res.send("Error")
   }
 })
})

【问题讨论】:

    标签: arrays node.js foreach cloudinary


    【解决方案1】:

    您需要使用异步回调库(如 async)同步上传,或转换为 Promise。我更喜欢 Promise,但 cloudinary 库需要一个 Promise 包装器。 幸运的是,node 8+ 带有一个内置的 Promisify 函数,可以将回调转换为 Promise。有了 Promise,您的代码将如下所示:

     const { promisify } = require('util')
    
     // Converts cloudinary callback interface into promise
     const uploadAsync = promisify(cloudinary.uploader.upload.bind(cloudinary.uploader))
    
     app.post("/newcampbook",(req,res)=>{
     new formidable.IncomingForm({ multiples: true }).parse(req, async (err,fields,file)=>{
       if(!err){     
         var pathArray = [];
         file.image.forEach(e =>{
           pathArray.push(e.path)
         })
         var savedUrl = [];
         for(let e of pathArray) {
           const { url } = await uploadAsync(e)
           savedUrl.push(url)
         }
         res.send("Testing to upload")
       }else{
        console.log(err);
        res.send("Error")
      }
     })
    })
    

    现在 ^ 该代码将一个接一个地同步执行每个上传。如果您想一次运行所有上传(这是您的原始代码正在执行的操作),那么它看起来像:

     const { promisify } = require('util')
    
     // Converts cloudinary callback interface into promise
     const uploadAsync = promisify(cloudinary.uploader.upload.bind(cloudinary.uploader))
    
     app.post("/newcampbook",(req,res)=>{
     new formidable.IncomingForm({ multiples: true }).parse(req, async (err,fields,file)=>{
       if(!err){     
         var pathArray = [];
         file.image.forEach(e =>{
           pathArray.push(e.path)
         })
         var savedUrl = (await Promise.all(pathArray.map(uploadAsync))).map(({ url }) => url)
         res.send("Testing to upload")
       }else{
        console.log(err);
        res.send("Error")
      }
     })
    })
    

    如果您想使用async 库,请查看parallelseries 函数,它们可以直接对回调执行相同的操作。

    【讨论】:

    • 我被困了两天,循环改变了每一个语法,试图完成它。你真棒。我可能需要阅读/观看一些有关 promise、async 和 await 的视频以了解更多信息。谢谢你,非常感谢
    • 没问题。如果它适合您,请考虑接受答案!祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    相关资源
    最近更新 更多