错误:尝试登录后发送标头后无法设置标头 [英] Error: Can't set headers after they are sent after tried to login

查看:113
本文介绍了错误:尝试登录后发送标头后无法设置标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题有很多问题,但是,我尝试了一些解决方案,但没有人起作用.

There are a lot of question with my issue but, I tried some of the solution and no one worked.

每次尝试登录时都会出现以下错误:

I'm getting the following error everytime I tried to do the login:

错误:发送标头后无法设置标头. 在validateHeader(_http_outgoing.js:491:11) 在ServerResponse.setHeader(_http_outgoing.js:498:3) 在ServerResponse.header(/home/ale/PycharmProjects/veople/webapp/functions/node_modules/express/lib/response.js:767:10) 在userService.checkUser(/home/ale/PycharmProjects/veople/webapp/functions/index.js:54:18) 在Object.firebase.auth.onAuthStateChanged.firebaseUser [下一个](/home/ale/PycharmProjects/veople/webapp/functions/auth.js:69:17) 在/home/ale/PycharmProjects/veople/node_modules/@firebase/util/dist/index.node.cjs.js:1361:22 在/home/ale/PycharmProjects/veople/node_modules/@firebase/util/dist/index.node.cjs.js:1465:21 在 在process._tickCallback(internal/process/next_tick.js:189:7)

Error: Can't set headers after they are sent. at validateHeader (_http_outgoing.js:491:11) at ServerResponse.setHeader (_http_outgoing.js:498:3) at ServerResponse.header (/home/ale/PycharmProjects/veople/webapp/functions/node_modules/express/lib/response.js:767:10) at userService.checkUser (/home/ale/PycharmProjects/veople/webapp/functions/index.js:54:18) at Object.firebase.auth.onAuthStateChanged.firebaseUser [as next] (/home/ale/PycharmProjects/veople/webapp/functions/auth.js:69:17) at /home/ale/PycharmProjects/veople/node_modules/@firebase/util/dist/index.node.cjs.js:1361:22 at /home/ale/PycharmProjects/veople/node_modules/@firebase/util/dist/index.node.cjs.js:1465:21 at at process._tickCallback (internal/process/next_tick.js:189:7)

这是我在节点中的index.js:

this is my index.js in node:

app.get('/user', (request, response) => {
    userService.checkUser( (firebaseUser) => {
        if (firebaseUser) {
            response.render('user', {firebaseUser});
        }else {
            // response.redirect('/');
            response.redirect('/');
        }
        response.set('Cache-Control', 'public, max-age=300, s-maxage=600');
        response.render('index');
    });

});


app.post("/login", (req, res) => {
 var userEmail = req.body.emailLogin;
 var userPassword = req.body.passwordLogin;
 console.log(userEmail, userPassword); //This log the email and the password

 userService.loginUser(
   userEmail,
   userPassword,

   (error, authData) => {
     if (error) {
       return res.status(401).send("Unauthorized");
     } else {
       res.redirect('/user');
       //console.log(authData);
       return authData
     }
   }
 );
});

在auth.js中,我有:

and in auth.js I have :

function loginUser(email, password, callback) {
     console.log(email, password);
     firebase
       .auth()
       .signInWithEmailAndPassword(email, password)
       .then((firebaseUser) => {
         // Success
         console.log("Ok, lovely I'm logged");
         callback(null, firebaseUser);
         //console.log(firebaseUser);
         return firebaseUser
       })
       .catch((error) => {

         callback(error);
       });
   }


function checkUser( callback ) {
   firebase
       .auth()
       .onAuthStateChanged(firebaseUser => {
           if(firebaseUser) {
               // console.log(firebaseUser);
               callback(firebaseUser);
               return firebaseUser;
           } else {
               console.log('pp');
               callback(null);
               return firebaseUser;
           }

       });
}

到目前为止,如果我在计算机上运行服务器,登录仍然可以,但是无论如何我都会在日志中看到错误,相反,如果我在firebase上部署项目,则无法使用登录功能.

So far if I run the server on my machine, the login works but I get anyway the error in my log, instead, if I deploy my project on firebase I can't use the login function.

可能是此错误.

有人可以向我解释我在做什么错吗?谢谢

Can anyone explain me what I'm doing wrong? Thankyou

EDIT

EDIT

使用@molda解决方案后,我得到了:

After aplying the @molda solution I'm getting:

错误:错误:发送标头后无法设置标头. 在validateHeader(_http_outgoing.js:491:11) 在ServerResponse.setHeader(_http_outgoing.js:498:3) 在ServerResponse.header(/home/ale/PycharmProjects/veople/webapp/functions/node_modules/express/lib/response.js:767:10) 在ServerResponse.location(/home/ale/PycharmProjects/veople/webapp/functions/node_modules/express/lib/response.js:884:15) 在ServerResponse.redirect(/home/ale/PycharmProjects/veople/webapp/functions/node_modules/express/lib/response.js:922:18) 在userService.checkUser(/home/ale/PycharmProjects/veople/webapp/functions/index.js:51:22) 在Object.firebase.auth.onAuthStateChanged.firebaseUser [下一个](/home/ale/PycharmProjects/veople/webapp/functions/auth.js:73:17) 在/home/ale/PycharmProjects/veople/node_modules/@firebase/util/dist/index.node.cjs.js:1361:22 在/home/ale/PycharmProjects/veople/node_modules/@firebase/util/dist/index.node.cjs.js:1465:21 在 在process._tickCallback(内部/进程/next_tick.js:189:7) 127.0.0.1--[06/Apr/2019:10:54:29 +0000]"GET/logout HTTP/1.1" 302 46"

error: Error: Can't set headers after they are sent. at validateHeader (_http_outgoing.js:491:11) at ServerResponse.setHeader (_http_outgoing.js:498:3) at ServerResponse.header (/home/ale/PycharmProjects/veople/webapp/functions/node_modules/express/lib/response.js:767:10) at ServerResponse.location (/home/ale/PycharmProjects/veople/webapp/functions/node_modules/express/lib/response.js:884:15) at ServerResponse.redirect (/home/ale/PycharmProjects/veople/webapp/functions/node_modules/express/lib/response.js:922:18) at userService.checkUser (/home/ale/PycharmProjects/veople/webapp/functions/index.js:51:22) at Object.firebase.auth.onAuthStateChanged.firebaseUser [as next] (/home/ale/PycharmProjects/veople/webapp/functions/auth.js:73:17) at /home/ale/PycharmProjects/veople/node_modules/@firebase/util/dist/index.node.cjs.js:1361:22 at /home/ale/PycharmProjects/veople/node_modules/@firebase/util/dist/index.node.cjs.js:1465:21 at at process._tickCallback (internal/process/next_tick.js:189:7) 127.0.0.1 - - [06/Apr/2019:10:54:29 +0000] "GET /logout HTTP/1.1" 302 46 "http://localhost:5000/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36 OPR/58.0.3135.127" [hosting] Rewriting / to local function app error: Error: Can't set headers after they are sent. at validateHeader (_http_outgoing.js:491:11) at ServerResponse.setHeader (_http_outgoing.js:498:3) at ServerResponse.header (/home/ale/PycharmProjects/veople/webapp/functions/node_modules/express/lib/response.js:767:10) at ServerResponse.send (/home/ale/PycharmProjects/veople/webapp/functions/node_modules/express/lib/response.js:170:12) at done (/home/ale/PycharmProjects/veople/webapp/functions/node_modules/express/lib/response.js:1004:10) at /home/ale/PycharmProjects/veople/webapp/functions/node_modules/consolidate/lib/consolidate.js:876:7 at /home/ale/PycharmProjects/veople/webapp/functions/node_modules/consolidate/lib/consolidate.js:161:5 at Promise._execute (/home/ale/PycharmProjects/veople/webapp/functions/node_modules/bluebird/js/release/debuggability.js:313:9) at Promise._resolveFromExecutor (/home/ale/PycharmProjects/veople/webapp/functions/node_modules/bluebird/js/release/promise.js:483:18) at new Promise (/home/ale/PycharmProjects/veople/webapp/functions/node_modules/bluebird/js/release/promise.js:79:10) at promisify (/home/ale/PycharmProjects/veople/webapp/functions/node_modules/consolidate/lib/consolidate.js:154:10) at Function.exports.handlebars.render (/home/ale/PycharmProjects/veople/webapp/functions/node_modules/consolidate/lib/consolidate.js:866:10) at /home/ale/PycharmProjects/veople/webapp/functions/node_modules/consolidate/lib/consolidate.js:181:27 at /home/ale/PycharmProjects/veople/webapp/functions/node_modules/consolidate/lib/consolidate.js:101:5 at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)

编辑2

EDIT 2

index.js:

function createDatauser(firebaseUid, name, lastname){
    const docRef = db.collection('users').doc(firebaseUid);

    const setData = docRef.set({
      first: name,
      last: lastname
    });
}

...

app.get('/', (request, response) => {
    userService.checkUser( (firebaseUser) => {
        if (firebaseUser) {
            console.log(firebaseUser.email)
        }else {
            console.log('not logged in');
        }
        // response.set('Cache-Control', 'public, max-age=300, s-maxage=600');
        response.render('index', {firebaseUser});
    });



});



...

app.post("/register", (req, res) => {
 const nameRegister = req.body.name;
    const surnameRegister = req.body.surname;
    const emailRegister = req.body.emailRegister;
    const passwordRegister = req.body.passwordRegister;
    const confirmPasswordRegister = req.body.confirmPasswordRegister;
    console.log(emailRegister, passwordRegister);

 userService.registerUser(
   emailRegister,
   passwordRegister,

   (error, authData) => {
     if (error) {
       return res.status(401).send("Unauthorized");
     } else {
       res.redirect('/user');
       console.log(authData);
       createDatauser(authData.user.uid, nameRegister, surnameRegister);
       return authData
     }
   }
 );
});

...

app.get("/logout", (req, res) => {
 userService.logoutUser();
 res.redirect('/');

});

auth.js



function registerUser(email, password, callback) {
      console.log(email, password);
      firebase
        .auth()
        .createUserWithEmailAndPassword(email, password)
        .then((firebaseUser) => {
          // Success
          //console.log("Ok, lovely I'm registred", firebaseUser);
          callback(null, firebaseUser);
          return firebaseUser
        })
        .catch((error) => {
          // Error Handling
          // var errorCode = error.code;
          // var errorMessage = error.message;
          //console.log(errorCode); //auth/user-not-found
          //console.log(errorMessage); //There is no user record corresponding to this identifier. The user may have been deleted.

          callback(error);
        });
    }


...


function logoutUser(){
    firebase.auth().signOut()
        .then(() => {
            return null;
      // Sign-out successful.
    })
        .catch((error) => {
            return error;
      // An error happened.
    });
}

推荐答案

多次调用response.render(...)会导致此错误.

Calling response.render(...) more then once will cause this error.

解决方案是删除第二个渲染调用,因此这两行:

Solution is to remove the second render call so these 2 lines:

response.set('Cache-Control', 'public, max-age=300, s-maxage=600');
response.render('index');

所以它应该像这样:

app.get('/user', (request, response) => {
    userService.checkUser( (firebaseUser) => {
        if (firebaseUser) {
            response.render('user', {firebaseUser});
        }else {
            response.redirect('/');
        }
        // what is the bellow code even for??
        // above you either render user page
        // or redirect to /
        // response.set('Cache-Control', 'public, max-age=300, s-maxage=600');
        // response.render('index');
    });
});

这篇关于错误:尝试登录后发送标头后无法设置标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆