简体   繁体   English

express-错误:发送标头后无法设置标头

[英]express - Error: Can't set headers after they are sent

Hello I tried everything but the following very simple code gives me the "Can't set headers after they are sent" error, I've been working on this for days you're input is highly appreciated. 您好,我尝试了所有操作,但是以下非常简单的代码为我提供了“发送后无法设置标题”错误,对于您输入的几天我一直在努力

  app.post('/login', function (req, res) {
      var emailpassed = req.body.email

      var shaObj = new jsSHA('SHA-256', 'TEXT')
      shaObj.update('zzzz' + req.body.password)
      var hash = shaObj.getHash('HEX')

      var params = {
        TableName: 'passengers',
        IndexName: 'emailpass',
        ProjectionExpression: 'password',
        KeyConditionExpression: '#yr = :yyyy',
        ExpressionAttributeNames: {
          '#yr': 'email'
        },
        ExpressionAttributeValues: {
          ':yyyy': emailpassed
        }
      }

      docClient.query(params, function (err, data) {
        if (err) {
          console.log('No such user found.1')

        } else {
          data.Items.forEach(function (item) {
            if (item.password != hash) {
              console.log('Incorrect password.1')
            } else {

              var payload = {id: item.pid, password: hash}
              var token = jwt.sign(payload, 'sa')

              if (token) {
                return res.json({token: token})

              }
            }

          })
          console.log('daaakey')

        }
      })
  return;
  }
  )

You are calling res.json() inside a .forEach() loop which means you can call it more than once any time data.Items.length is more than 1 and some other conditions are met. 您正在.forEach()循环内调用res.json() ,这意味着您可以在data.Items.length大于1且满足某些其他条件时data.Items.length调用它。 Remember that when you do return res.json() , the return is returning from the .forEach() callback and thus the .forEach() loop continues to run and the callback an be called again. 请记住,当您确实return res.json() ,该return是从.forEach()回调返回的,因此.forEach()循环继续运行,并且再次调用了该回调。 The error you are seeing is caused by attempting to send more than one response to a given request which is not permitted. 您看到的错误是由于尝试向给定请求发送多个响应而导致的,这是不允许的。

You need to restructure your code flow such that your either your .forEach() loop gathers input (often in an array) and then sends one response containing all the data after the loop is done or if you only intend to send a response on the first token found, then you probably want to switch to a regular for loop so you can break out of the loop after you send the response (to avoid sending another one) with a return or break . 您需要重组代码流,以使您的.forEach()循环收集输入(通常在数组中),然后在循环完成后发送一个包含所有数据的响应,或者仅打算在循环上发送响应。找到第一个令牌,然后您可能要切换到常规for循环,以便在发送带有returnbreak的响应(避免发送另一个)后可以退出循环。 It is not clear from your code which of these scenarios is your likely intention (sending only the first data or accumulating all the data and sending it all). 从您的代码中尚不清楚您可能打算使用哪种方案(仅发送第一个数据或累积所有数据并全部发送)。

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

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