错误[HTTP标题发送错误]:将标头发送到位于新节点的客户端后无法设置标头错误(node:internal/errors:371:5)

cdmah0mi  于 2023-03-01  发布在  Node.js
关注(0)|答案(2)|浏览(225)

我在登录发生后收到此错误。代码正常工作,但在用户返回后收到此错误。

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:371:5)
    at ServerResponse.setHeader (node:_http_outgoing:576:11)
    at ServerResponse.header (C:\Users\Oswald\Desktop\Folders\nodeProjects\practisefolder\nodeWebAppTutorial\node_modules\express\lib\response.js:771:10)
    at ServerResponse.location (C:\Users\Oswald\Desktop\Folders\nodeProjects\practisefolder\nodeWebAppTutorial\node_modules\express\lib\response.js:888:15)
    at ServerResponse.redirect (C:\Users\Oswald\Desktop\Folders\nodeProjects\practisefolder\nodeWebAppTutorial\node_modules\express\lib\response.js:926:18)
    at allFailed (C:\Users\Oswald\Desktop\Folders\nodeProjects\practisefolder\nodeWebAppTutorial\node_modules\passport\lib\middleware\authenticate.js:139:20)
    at attempt (C:\Users\Oswald\Desktop\Folders\nodeProjects\practisefolder\nodeWebAppTutorial\node_modules\passport\lib\middleware\authenticate.js:174:28)
    at Strategy.strategy.fail (C:\Users\Oswald\Desktop\Folders\nodeProjects\practisefolder\nodeWebAppTutorial\node_modules\passport\lib\middleware\authenticate.js:296:9)
    at verified (C:\Users\Oswald\Desktop\Folders\nodeProjects\practisefolder\nodeWebAppTutorial\node_modules\passport-local\lib\strategy.js:82:30)

    at C:\Users\Oswald\Desktop\Folders\nodeProjects\practisefolder\nodeWebAppTutorial\src\config\passport.js:17:32

守则

const usersRepo = require('../repositories/usersRepo');
const { Strategy } = require('passport-local');

function initialize(passport) {

    passport.use('local.signIn', new Strategy({
        usernameField: 'username',
        passwordField: 'password'
    }, (username, password, done) => {
        (async function ValidateUser() {
            try {
                const users = await usersRepo.getData({ username: username });
                users.forEach((user) => {
                    if (user.username === username && user.password === password) {
                        return done(null, user)
                    } else {
                        return done(null, false, { message: "user not found" })
                    }
                })

            } catch (error) {
                done(error, false)
            }

        }())
    }))

    passport.serializeUser((user, done) => {
        done(null, user);
    });
    passport.deserializeUser((user, done) => {
        done(null, user);
    });
}
module.exports = initialize
xj3cbfub

xj3cbfub1#

问题是.forEach中的return语句没有停止迭代,因为它只是从当前forEach回调返回。但这不是唯一的问题,因为每次找不到用户时都会调用done()回调。您可能正在寻找类似下面的内容:

const userFound = users.find(user => user.username === username && user.password === password);
if (userFound) {
     done(null, user); 
} else {
     done(null, false, {message: "user not found"});
}
3pmvbmvn

3pmvbmvn2#

在我的情况下,我得到了如下错误

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:371:5)
    at ServerResponse.setHeader (node:_http_outgoing:576:11)

为了解决该错误,我在res关键字之前添加了return
这是解决错误之前的代码

await db.user_message.create(userMessage).then((data) => {
     res.send(data);
  });

  db.user_message.create(userMessage).then((data) => {
     res.send(data);
  });

下面是解决错误后的代码

await db.user_message.create(userMessage).then((data) => {
    return res.send(data);
  });

  db.user_message.create(userMessage).then((data) => {
    return res.send(data);
  });

相关问题