【问题标题】:Firebase cloud functions not logging properlyFirebase 云功能未正确记录
【发布时间】:2017-06-22 14:41:43
【问题描述】:

我正在创建一个社交应用程序,朋友们可以在其中通过他们的通讯录找到彼此。本质上,我有两个长长的电话号码列表,我想使用 firebase-cloud-function 查找两个列表中存在的所有号码。

为此,我将用户的所有联系信息发布到 Firebase 实时数据库并触发云功能 (.onWrite)。云函数获取刚刚发布的所有电话号码,然后检索我的数据库中所有经过验证的电话号码,然后需要交叉引用两个列表以查找所有匹配项。

这是我的代码...

 exports.crossReferenceContacts = functions.database.ref('/cross-ref-contacts/{userId}').onWrite(event => {

    //if it already existed then this function triggered beacuse we are calling set(null) and thus we dont want to proceed
    if (event.data.previous.exists()) {
        console.log("PREVIOUSLY EXISTED. RETURN NULL!!!");
        return null;
    }

    const userId = event.params.userId;
    const userContacts = event.data.val();

    var contactsOnPhone = [];
    var contactsVerifiedInDatabase =[];
    var matchedContacts= [];

    for (var key in userContacts){
        let contact = new Object();
        contact.contactName = userContacts[key];
        contact.number = key;
        contactsOnPhone.push(contact);
    };

    var verifiedNumsRef = event.data.adminRef.root.child('verified-phone-numbers');
    return verifiedNumsRef.once('value', function(snapshot) {
        const verifiedUsers = snapshot.val();

        for (key in verifiedUsers){
            const number = key
            const userInfo = verifiedUsers[key];
            for (key in userInfo){
                const uid = key;
                const username = userInfo[key];

                var verifiedContact = new Object();
                verifiedContact.name = username;
                verifiedContact.uid = uid;
                verifiedContact.number = number;
                contactsVerifiedInDatabase.push(verifiedContact);
            };

        };

        var verifiedNumbers = contactsVerifiedInDatabase.map(function(x){
            return x.number;
        });
        var contactNumbers = contactsOnPhone.map(function(x){
            return x.number;
        });

      //THIS LINE DOES NOT EVEN GET LOGGED UNLESS I COMMENT OUT THE FOR-LOOP BELOW, ONLY THEN DOES THIS LINE GETS LOGGED
        console.log('got ', verifiedNumbers.length, ' verified numbers along with', contactsVerifiedInDatabase.length, ' verfieid contacts.  Also got', contactNumbers.length, ' phone numbers along with ', contactsOnPhone.length, ' phone contacts');



        for (i = 0; i < contactNumbers.length; i++){

            console.log("i = ", i);

            if (verifiedNumbers.includes(contactNumbers[i])) {
                console.log("GOT A MATCH WITH # ", contactNumbers[i]);
            } else {
                console.log("No mATCH FOR # ", contactNumbers[i]);
            };
        };

        return null;        

    });


});

由于某种原因,当我在终端中运行 firebase functions:log 时,日志不完整。这是日志...

为什么每次循环都没有打印日志?它不应该一直打印 i = 0 到 i = 409 吗?为什么总是从 393 开始?而且该功能正在以“超时”状态完成,这是为什么呢?

【问题讨论】:

  • 我注释掉了所有内容,只是尝试运行for(i = 0; i &lt; 500; i++){console.log(i);};,但firebase日志仍然只显示466到499。每次它只记录最后33个“console.log”
  • 试试这个命令firebase functions:log -n 500
  • 成功了!谢谢你。你在哪里读到如何做到这一点?我在 Firebase 文档中找不到任何内容
  • 请看答案

标签: node.js firebase firebase-realtime-database


【解决方案1】:

firebase functions:log 默认只能输出一定数量的行。看起来大约40左右。

您可以运行firebase help functions:log 来获取告诉您的命令行选项:

Usage: functions:log [options]

read logs from deployed functions

Options:

-h, --help               output usage information
--only <function_names>  only show logs of specified, comma-seperated functions (e.g. "funcA,funcB")
-n, --lines <num_lines>  specify number of log lines to fetch
--open                   open logs page in web browser

所以只需运行 firebase functions:log -n 500 即可获得 500 行。他们没有指定它是第一个还是最后一个 500,可能是最后一个。不过也够了。

Functions Help

【讨论】:

    猜你喜欢
    • 2020-11-19
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2019-10-15
    相关资源
    最近更新 更多