【发布时间】:2018-11-06 14:18:58
【问题描述】:
我可以在 Firebase 中编写一个接受 POST 请求的 HTTPS 端点。我可以使用它并从中得到响应。我可以整天 console.log() 。
我记录每一步,因为它决定如何处理请求,但是一旦我尝试保存到 Firestore,整个端点就会静默。步骤 1-7 曾经愉快地使用 console.log(),现在步骤 8 把整个事情吸进了一个黑洞。
我认为我在使用 async/await/promises 时做错了,所以我用一个愚蠢的 setTimeout 承诺替换了“第 8 步”。这很好用:我的承诺会在 2000 毫秒后写入 console.log。然后我认为我保存的数据有问题(因为这一切都是在我尝试修复有关在 Firestore 中存储日期的警告时开始的)。我用 {test: "Test"} 替换了我的数据 - 又是黑洞。
似乎只是保存到数据库的行为会创建一个黑洞。但仅在这一端点上。我有其他人做类似的事情就好了。
我已经研究这个问题好几天了,如果有任何帮助,我们将不胜感激。
app.post("/webhook", async (req: express.Request, res: express.Response) => {
if(req.body.token !== functions.config().service.verification_token)
throw new Error("Unauthorized access");
if(req.body.type === "url_verification")
{
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('CHALLENGE_RESPONSE');
return;
}
res.end("OK");
if(DEBUG) { console.log("Received a request", req.body); }
if(req.body.type === "event_callback")
{
if(DEBUG) { console.log("This request appears to be an event"); }
if(req.body.event.type === "message")
{
if(DEBUG) { console.log("This event appears to concern a message"); }
if(shouldIgnoreMessage(req.body.event))
{
if(DEBUG) { console.log("Actually, this event is not something we care about. We're done here."); }
return;
}
if(DEBUG) { console.log("Whoa-ho! This is a message from a user! We have to deal with this."); }
saveToFirebase(req.body).catch(err => console.error(err));
}
}
});
【问题讨论】:
标签: node.js firebase google-cloud-firestore google-cloud-functions