繁体   English   中英

Javascript for in loop 无法获取 S3 预签名 URL

[英]Javascript for in loop not working for getting S3 pre-signed urls

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

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

当尝试在 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()
    }
  }
)



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

for (const user of users) {

for in 循环为您提供索引。

for of 循环为您提供 object

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM