gpt4 book ai didi

javascript - 如何在 if 语句所在的 for 循环之外返回 if 语句的 else

转载 作者:太空宇宙 更新时间:2023-11-04 03:00:25 24 4
gpt4 key购买 nike

我有一个sequelize查询,它查找所有具有can_upload = 1属性的用户,该属性允许他们将文件上传到数据库。为此,我循环遍历所有具有 can_upload = 1 属性的用户,然后我有一个 if 语句,该语句检查请求 header 中发送的用户的电子邮件是否与 for 循环中迭代的任何单个匹配。

这部分很好,但我无法在这个 for 循环中返回 else{},因为如果 req header 中的电子邮件不等于迭代的第一个值,则循环将停止,else{} 语句将返回,而不检查循环中的其他值。

users.findAll({where: {can_upload: 1}}).then(projectUsers=>{ 
var email = "";
for (let i=0; i<projectUsers.length;i++){
email=projectUsers[i].dataValues.email_address;
if (email === req.headers.email_address) {
//do a bunch of stuff
res.status(200).send{`message: success`};
}
}
//somehow return the else here which would return
res.status(401).send{`message: user is not authorized to upload`};
)};

最佳答案

你可以这样做:

users.findAll({where: {
can_upload: 1, 'dataValues.email_address': req.headers.email_address}
}).then(projectUsers=>{ // do whatever you need here // }

关于javascript - 如何在 if 语句所在的 for 循环之外返回 if 语句的 else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58307374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com