【发布时间】: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