【问题标题】:Error [ERR_HTTP_HEADERS_SENT] after change in database更改数据库后出现错误 [ERR_HTTP_HEADERS_SENT]
【发布时间】:2019-07-08 18:37:23
【问题描述】:

如果用户付费与否,我想检查 Firestore 数据库。所以每次我去/paid 路线时,我都会在数据库中检查他是否付款。 但是如果他要求路线并且假设他没有付款。然后我更改了数据库以便他付款然后我收到错误

我尝试检查我是否发送了两次标头,但我没有找到任何东西。

一旦我重新启动服务器,应用程序就会再次运行。

这是 app.js 代码:

app.get("/paid", verifyUser, (req, res) => {
  const claims = res.locals.decodedClaims;
  const uid = claims.uid;
  const email = claims.email;
  const userRef = db.collection("users").doc(uid);

  userRef.get().then(docSnapshot => {
    if (docSnapshot.exists) {
      userRef.onSnapshot(doc => {
        // do stuff with the data
        if (doc.data().paid) {
          res.send("paid");
          return;
        }

        res.send("Didn't pay");
      });
    } else {
      userRef.set({ paid: false, name: email }); // create the document

      res.send("<h1>hello world</h1><br>" + uid);
    }
  });
});

verifyUser middleWare(只是为了澄清):

//import firebase admin module
const admin = require("firebase-admin");

module.exports = {
  // check if cookie is available and if cookie is valid
  verifyUser: (req, res, next) => {
    var sessionCookie = req.cookies.session || "";

    if (sessionCookie) {
      admin
        .auth()
        .verifySessionCookie(sessionCookie, true)
        .then(function(decodedClaims) {
          //decodedClaims is not nessecary now
          res.locals.decodedClaims = decodedClaims;
          return next();
        })
        .catch(function(err) {
          // Redirect to login page on error.
          console.log(err);

          res.redirect("/loggedOutFail");
        });
    } else {
      // Redirect to login page when no session cookie available.
      res.redirect("/loggedOutNoCookie");
    }
  }
};

这是错误:

_http_outgoing.js:470
    throw new ERR_HTTP_HEADERS_SENT('set');
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:470:11)
    at ServerResponse.header (C:\Users\mendi\Desktop\firebase-auth\node_modules\express\lib\response.js:771:10) 
    at ServerResponse.contentType (C:\Users\mendi\Desktop\firebase-auth\node_modules\express\lib\response.js:599:15)
    at ServerResponse.send (C:\Users\mendi\Desktop\firebase-auth\node_modules\express\lib\response.js:145:14)   
    at userRef.onSnapshot.doc (C:\Users\mendi\Desktop\firebase-auth\app.js:306:13)
    at DocumentWatch.watch.onSnapshot [as onNext] (C:\Users\mendi\Desktop\firebase-auth\node_modules\@google-cloud\firestore\build\src\reference.js:404:21)
    at DocumentWatch.pushSnapshot (C:\Users\mendi\Desktop\firebase-auth\node_modules\@google-cloud\firestore\build\src\watch.js:519:18)
    at DocumentWatch.onData (C:\Users\mendi\Desktop\firebase-auth\node_modules\@google-cloud\firestore\build\src\watch.js:403:26)
    at BunWrapper.currentStream.on (C:\Users\mendi\Desktop\firebase-auth\node_modules\@google-cloud\firestore\build\src\watch.js:372:26)
    at BunWrapper.emit (events.js:189:13)

结果应该是每次请求我都可以更改数据库中的数据并且一切仍然正常。

【问题讨论】:

    标签: javascript node.js database google-cloud-firestore http-headers


    【解决方案1】:

    原来我使用了onSnapshot method 来检测数据库中的每一个变化。因此,如果我在发送标头后更改数据库,我会收到错误消息。

    这是app.js的正确代码:

       app.get("/paid", verifyUser, (req, res) => {
      const claims = res.locals.decodedClaims;
      const uid = claims.uid;
      const email = claims.email;
      const userRef = db.collection("users").doc(uid);
    
      userRef
        .get()
        .then(docSnapshot => {
          // check if user document exists, if not create it
          if (!docSnapshot.exists) {
            userRef.set({ paid: false, name: email });
            res.send("User Created" + res.locals.decodedClaims.uid);
            return;
          }
    
          //check if user paid
          if (docSnapshot.data().paid) {
            res.send("Paid");
            return;
          } else {
            res.send("Didn't pay!");
            return;
          }
        })
        .catch(err => {
          console.log(5);
          throw err;
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2013-12-02
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      相关资源
      最近更新 更多