我想在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)
结果应该是在每次请求时我都可以更改数据库中的数据,并且一切仍然正常。
答案 0 :(得分:0)
事实证明,我使用了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;
});
});