MikeG
MikeG

Reputation: 4044

Firebase cloud functions not logging properly

I am creating a social app in which friends are able to find each other via their address book. Essentially I have two long lists of phone numbers, and I would like to find all numbers that exist on both lists using a firebase-cloud-function.

To do this, I have the user's post all their contact information to Firebase Realtime Database and a cloud function is triggered (.onWrite). The cloud function gets all the phone numbers that were just posted, and then it retrieves all the verified phone numbers in my database, and then it needs to cross-reference the two lists to find all matches.

Here is my code...

 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;        

    });


});

For some reason when I run firebase functions:log in my terminal, the log is incomplete. Here is the log...functions log

Why isn't the log printing everytime the for loop is looped? Shouldn't it print i = 0 all the way to i = 409?? why does it always start at 393?? And also the function is finishing with status "timeout", why is that?

Upvotes: 4

Views: 3700

Answers (1)

theblindprophet
theblindprophet

Reputation: 7937

firebase functions:log must only output some certain number of lines by default. Looks about 40 or so.

You can run firebase help functions:log to get the command line options which tells you:

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

So just run firebase functions:log -n 500 to get the 500 lines. They don't specify if its the first or last 500, probably last. But its enough.

Functions Help

Upvotes: 6

Related Questions