簡體   English   中英

將標頭發送到客戶端后無法設置標頭-NodeJs

[英]Cannot set headers after they are sent to the client - NodeJs

我正在編寫一個 react.js 應用程序,比如一個人們可以在其中發布帖子的博客,就像在 Medium 平台中一樣。 但是,當我的用戶嘗試登錄他們的帳戶時,我的快速服務器返回一個錯誤,指出應用程序在發送標頭后無法返回標頭,但我不知道如何解決這個問題......

我的代碼:

app.post("/users/login", (req, res) => {

     var ReqUsername = req.body.username;
     var ReqPassword = req.body.password;

     userModel.find((err, docs) => {
          if(!err) {

               console.log(docs)

               docs.forEach(user => {
                    console.log(user)

                    var encryptedUsername = user.username;
                    var encryptedPassowrd = user.password;

                    var bytesUsername  = CryptoJS.AES.decrypt(encryptedUsername, <SECRET KEY>);
                    var bytesPassword  = CryptoJS.AES.decrypt(encryptedPassowrd, <SECRET KEY>);

                    var decryptedUsername = bytesUsername.toString(CryptoJS.enc.Utf8);
                    var decryptedPassword = bytesPassword.toString(CryptoJS.enc.Utf8);

                    if(decryptedUsername == ReqUsername) {
                         if(decryptedPassword == ReqPassword) {
                              res.json({
                                   sucesso: true,
                                   mensagem:"Login feito com sucesso",
                                   userId: user.userId
                              }).status(202)
                         }
                         else {
                              res.json({
                                   sucesso: false,
                                   mensagem:"Por favor, confira as suas informações e tente login novamente"
                              }).status(404)
                         }
                    }
                    else {
                         res.json({
                              sucesso: false,
                              mensagem:"Por favor, confira as suas informações e tente login novamente"
                         }).status(404)
                    }

               })
          }
          else {
               res.json("Error on check user database").status(500)
          }
     })
})

使用后登錄 url 后,我總是收到此錯誤:

2021-02-20T14:16:38.847139+00:00 app[web.1]: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
2021-02-20T14:16:38.847140+00:00 app[web.1]:     at ServerResponse.setHeader (_http_outgoing.js:558:11)
2021-02-20T14:16:38.847140+00:00 app[web.1]:     at ServerResponse.header (/app/node_modules/express/lib/response.js:771:10)
2021-02-20T14:16:38.847141+00:00 app[web.1]:     at ServerResponse.send (/app/node_modules/express/lib/response.js:170:12)
2021-02-20T14:16:38.847141+00:00 app[web.1]:     at ServerResponse.json (/app/node_modules/express/lib/response.js:267:15)
2021-02-20T14:16:38.847142+00:00 app[web.1]:     at /app/main.js:79:35
2021-02-20T14:16:38.847142+00:00 app[web.1]:     at Array.forEach (<anonymous>)
2021-02-20T14:16:38.847142+00:00 app[web.1]:     at /app/main.js:65:21
2021-02-20T14:16:38.847143+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/model.js:4857:16
2021-02-20T14:16:38.847143+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/model.js:4857:16
2021-02-20T14:16:38.847144+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16
2021-02-20T14:16:38.847144+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/model.js:4880:21
2021-02-20T14:16:38.847144+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/query.js:4399:11
2021-02-20T14:16:38.847145+00:00 app[web.1]:     at /app/node_modules/kareem/index.js:136:16
2021-02-20T14:16:38.847145+00:00 app[web.1]:     at processTicksAndRejections (internal/process/task_queues.js:75:11)
2021-02-20T14:16:38.847146+00:00 app[web.1]: Emitted 'error' event on Function instance at:
2021-02-20T14:16:38.847146+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/model.js:4859:13
2021-02-20T14:16:38.847146+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16
2021-02-20T14:16:38.847147+00:00 app[web.1]:     [... lines matching original stack trace ...]
2021-02-20T14:16:38.847147+00:00 app[web.1]:     at processTicksAndRejections (internal/process/task_queues.js:75:11) {
2021-02-20T14:16:38.847147+00:00 app[web.1]:   code: 'ERR_HTTP_HEADERS_SENT'
2021-02-20T14:16:38.847148+00:00 app[web.1]: }

我試圖取消一些響應,但它無法正常工作。 你們能幫我解決這個錯誤嗎?

僅僅take off some responses是不夠的,每個請求必須只有一個響應。

app.post("/users/login", (req, res) => {
     var ReqUsername = req.body.username;
     var ReqPassword = req.body.password;
     userModel.find((err, docs) => {
          if(!err) {
               const user = docs.find(user => {
                    var bytesUsername  = CryptoJS.AES.decrypt(user.username, SECRET KEY);
                    var decryptedUsername = bytesUsername.toString(CryptoJS.enc.Utf8);
                    return decryptedUsername === ReqUsername;
               });
               
               if(!user) {
                    res.json({
                        sucesso: false,
                        mensagem:"Por favor, confira as suas informações e tente login novamente"
                    }).status(404)
               } else {
                    var bytesPassword  = CryptoJS.AES.decrypt(user.password, SECRET KEY);
                    var decryptedUsername = bytesUsername.toString(CryptoJS.enc.Utf8);
                    if(decryptedPassword == ReqPassword) {
                        res.json({
                            sucesso: true,
                            mensagem:"Login feito com sucesso",
                            userId: user.userId
                        }).status(202)
                    } else {
                        res.json({
                            sucesso: false,
                            mensagem:"Por favor, confira as suas informações e tente login novamente"
                        }).status(404)
                    }
               }
          } else {
               res.json("Error on check user database").status(500)
          }
     })
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM