【问题标题】:Javascript for in loop not working for getting S3 pre-signed urls循环中的 Javascript 无法获取 S3 预签名的网址
【发布时间】:2021-05-13 22:44:14
【问题描述】:

我无法让下面的代码工作。

我正在查询用户对象,然后 对于返回的每个 用户对象,我需要从其 idKey 和 selfieKey 属性中获取 2 个预签名的 url,然后将用户对象及其各自的 pre 发送回-签名的网址。

当尝试在 for in 循环中执行此操作时,我在记录数组 keyArray 时得到 [undefined, undefined] ,因此代码在到达 S3 方法之前在此处失败。

任何帮助将不胜感激。谢谢

router.post("/api/verification/check", auth, async (req, res) => {
    try {
      const users = await User.find({ // Gets 1 or more User objects })

      let usersWithUrls = [] // add results for each loop iteration 

      for (const user in users) {
      
          const keyArray = [user.idKey, user.selfieKey]
          console.log(keyArray)
        
          const urlArray = await Promise.all(
            keyArray.map((key) =>
              S3.getSignedUrlPromise("getObject", {
                Bucket: "app-bucket",
                Key: key,
                Expires: 30,
              })
            )
          )
          const idUrl = urlArray[0]
          const selfieUrl = urlArray[1]

          usersWithUrls.push({ user, idUrl, selfieUrl })
      }

      if (users) {
        return res.send(usersWithUrls)
      }
    } catch (err) {
      res.status(400).send()
    }
  }
)



【问题讨论】:

    标签: javascript express


    【解决方案1】:

    尝试将您的 for in 更改为 for of。

    for (const user of users) {
    

    for in 循环为您提供索引。

    for of 循环给你对象

    【讨论】:

      猜你喜欢
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多