2017-06-22 51 views
1

我正在創建一個社交應用,其中的朋友可以通過他們的地址簿找到彼此。基本上,我有兩個長長的電話號碼列表,我想使用firebase雲功能查找這兩個列表中存在的所有號碼。Firebase雲計算功能未正確記錄

爲此,我將用戶的所有聯繫信息發佈到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,日誌是不完整的。這裏是日誌... functions log

爲什麼日誌打印每次都是for循環循環?不應該打印i = 0直到i = 409?爲什麼它始終始於393?此外,該功能正在結束狀態「超時」,爲什麼呢?

+0

我註釋掉一切,只是試圖運行'爲(I = 0;我<500;我++){的console.log(I);};'和靜止的火力日誌只顯示466通過499.每次只記錄最後33個「console.log的」 – MikeG

+1

試試這個命令'firebase函數:log -n 500' – theblindprophet

+0

工作正常!謝謝。你在哪裏閱讀如何做到這一點?我無法在Firebase文檔中找到任何內容 – MikeG

回答

2

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