我正在创建一个社交应用,朋友可以通过他们的地址簿找到对方。基本上我有两个很长的电话号码列表,我想使用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
时,日志不完整。这是日志...
为什么每次for循环被循环时都没有日志打印?不应该打印i = 0一直到i = 409 ??为什么总是从393开始?而且功能正在完成状态&#34;超时&#34;,为什么会这样?
答案 0 :(得分:4)
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,可能是最后一个。但它足够了。