Node.js Sequelize:如何将我的解密值与req.body进行比较

Node.js Sequelize:如何将我的解密值与req.body进行比较,node.js,sequelize.js,Node.js,Sequelize.js,我试图找到一个用户,其mobilenumber=req.body.mobilenumber。我的问题是数据库中的mobilenumber字段被加密 当我的用户在前端输入他们的mobilenumber时,我将解密数据库中的mobilenumber并使用我的req.body.mobilenumber进行检查,如何实现这一点 我使用Sequelize作为我的ORM 我第一次做这个: User.findOne({ where: {

我试图找到一个用户,其mobilenumber=req.body.mobilenumber。我的问题是数据库中的mobilenumber字段被加密

当我的用户在前端输入他们的mobilenumber时,我将解密数据库中的mobilenumber并使用我的req.body.mobilenumber进行检查,如何实现这一点

我使用Sequelize作为我的ORM

我第一次做这个:

User.findOne({
               where: {
                       mobilenumber: req.body.countrycode + req.body.mobilenumber,
                       },
               }).then((user) => {
                                   if (user) {
                                               res
                                               .status(400)
                                               .send({ error: "A user with the given mobile number exists." });
                                               return;
                                             }
                                   res.status(201).send(user);
                                 });
然后我注意到我数据库中的值被加密,因此所有mobilenumbers都通过了,所以我尝试了以下方法,但不起作用:

router.post(
"/",
(req, res) => {      
User.findAll().then((user)=>{

   console.log(user.map(x=>x.mobilenumber))

   const user_mobilenumber=user.map(x=>x.mobilenumber);

   const new_user =req.body.countrycode + req.body.mobilenumber;

const aes256gcm = (key) => {

    const decrypt = (enc) => {
      enc = Buffer.from(enc, "base64");
      const iv = enc.slice(enc.length - 28, enc.length - 16);
      const tag = enc.slice(enc.length - 16);
      enc = enc.slice(0, enc.length - 28);
      const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
      decipher.setAuthTag(tag);
      let str = decipher.update(enc, null, 'utf8');
      str += decipher.final('utf8');
      return str;
    };
  
    return {
      decrypt,
    };
  };

  const aesCipher = aes256gcm(key);

   user_mobilenumber.forEach((x)=>{
     const y = aesCipher.decrypt(x)
     console.log(y)

     if(new_user===y){
      res.status(400).send({ error: "A user with the given mobile number exists." });
     }
   res.status(201).send(new_user);


   })

 })
});
我不断地发现这个错误:


(节点:6456)未经处理的PromiserEjectionWarning:错误[ERR\u HTTP\u HEADERS\u SENT]:发送到客户端后无法设置头文件

好的,我知道了,所以问题出在代码的前半部分,您要发送两次响应,即

if(new_user===y){
      res.status(400).send({ error: "A user with the given mobile number exists." });
     }
   res.status(201).send(new_user);
从第一个if返回,或将第二个状态发送到else块,即 像这样的

for (const x of user_mobilenumber) {
  const y = aesCipher.decrypt(x);
  console.log(y);
  if (new_user === y) {
    res.status(400).send({ error: "A user with the given mobile number exists." });
    return;
  }
}

res.status(201).send(new_user);

您似乎不止一次尝试发送/设置响应标题,这是不可能的,您可以共享完整的代码吗?@RaghuChahar谢谢您的评论,我已经用完整的代码更新了问题,只是缺少了'res.status(201).send(new_user);`在底部,是时候使用
async
vs.
then()
了,也是时候控制缩进了。@tadman我应该在哪里等待?你使用
async
函数和
let result=await x()
而不是
x。然后(result=>{…}
。不幸的是,我仍然收到同样的错误。好的,您不能发送多个响应,请删除forEach并只发送一次响应,检查更新的答案。问题是user_mobilenumber通过执行'x=user_mobilenumber[0];`我只是从ArrayCheck更新的答案中选取了第一个元素,现在看起来很好。不,
for(user\u mobilenumber的const x)
可以同步,也就是说,您可以在两者之间进行中断,但forEach是异步的,也就是说,我们不能在两者之间进行中断。