【问题标题】:Firebase Cloud Function no console.log when admin.firestore() is called调用 admin.firestore() 时 Firebase Cloud Function 没有 console.log
【发布时间】:2020-03-03 13:33:42
【问题描述】:

我有一个 webhook 可以在云功能中接收 facebook messenger 事件,如下所示:

export const facebookMessengerHook = functions.https.onRequest(async (req: express.Request, res: express.Response) => {
    console.log(req);
    console.log(req.method);
    console.log(req.body);
    if (req.method == "POST") {
        const body = req.body;

        console.log(body);
        // Checks this is an event from a page subscription
        if (body.object === 'page') {
            res.status(200).send('EVENT_RECEIVED');

            // Iterates over each entry - there may be multiple if batched
            for (const entry of body.entry) {
                // will only ever contain one message, so we get index 0
                const webhook_data = entry.messaging[0];

                console.log(webhook_data);
                try {
                    // v THAT PART HERE v
                    const user = await admin.firestore().collection('users')
                        .where('facebookMessengerId', '==', webhook_data.sender.id)
                        .get();
                    // ^ THAT PART HERE ^
                    console.log(user);
                } catch (e) {
                    console.log('No user');
                }
            }
        }
        else {
            // Returns a '404 Not Found' if event is not from a page subscription
            res.sendStatus(404);
        }
    }
});

它不会记录任何东西,除非我在 sn-p 中注释掉标记的部分。

有人可以向我解释一下为什么以及如何解决这个问题,因为我需要调用 firestore 并且还需要 console.log 用于调试目的?

感谢您的帮助!

【问题讨论】:

  • 这很奇怪。我了解您通常会获取其他所有内容的日志。所以不只记录您在try 语句中得到的user
  • @PabloAlmécijaRodríguez 不,这不会改变任何事情。

标签: node.js typescript firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

问题很可能来自于这样做

res.status(200).send('EVENT_RECEIVED');

您实际上向 Cloud Function 平台表明,可以在其余异步工作(对 get() 方法的调用集)完成之前终止 Cloud Function。请参阅以下官方video 表格更多详细信息。换句话说,Cloud Function 在get() 方法返回的promise 被解析之前终止。

所以你应该修改你的代码如下:

    //....
    if (body.object === 'page') {


        // Iterates over each entry - there may be multiple if batched
        for (const entry of body.entry) {
            // will only ever contain one message, so we get index 0
            const webhook_data = entry.messaging[0];

            console.log(webhook_data);
            try {

                const user = await admin.firestore().collection('users')
                    .where('facebookMessengerId', '==', webhook_data.sender.id)
                    .get();

                console.log(user);


            } catch (e) {
                console.log('No user');
                //Here throw an error to be catched at an upper level
            }
        }

        res.status(200).send('EVENT_RECEIVED');
    }
    //....

请注意,您可以使用Promise.all(),因为您向数据库发出了一系列提取。但是使用您的代码无法确认这一点,因为它没有显示这些提取的确切用途。

【讨论】:

  • 是的,你是对的,谢谢。你有什么解释,为什么它在没有等待调用的情况下正常运行?我记得异步函数实际上是同步运行的,直到第一次等待调用(或类似的调用),所以它肯定与此有关。我只是想我可以将答案发送到 facebook 并在此之后处理剩下的事情,因为 facebook 对答案的速度非常苛刻(我认为只有 2 秒或类似的时间)。
  • “你有什么解释,为什么它在没有等待调用的情况下正常运行”是什么意思?
  • 因此,如果我注释掉 await 调用,所有内容都会被记录下来,并且函数会正确终止,即使 res.status(200).send('EVENT_RECEIVED'); 在所有内容之前也是如此。就在我添加任何等待的内容时,我没有得到 console.logs。你知道为什么会这样吗?
  • 如果您删除等待,云函数中将不再有任何异步内容,并且(在大多数情况下,如果不是全部)它有时间在被杀死之前执行控制台日志。
  • 我不太明白其中的原因。
猜你喜欢
  • 2020-01-01
  • 1970-01-01
  • 2021-01-06
  • 2020-09-16
  • 1970-01-01
  • 2018-01-24
  • 2020-06-19
  • 2019-10-09
  • 2020-09-05
相关资源
最近更新 更多