Mik*_*keG 4 node.js firebase firebase-realtime-database
我正在创建一个社交应用程序,朋友可以在其中通过通讯录找到彼此。本质上,我有两个很长的电话号码列表,并且我想使用firebase-cloud-function查找两个列表中都存在的所有号码。
为此,我将用户的所有联系信息发布到Firebase Realtime数据库,并触发了云功能(.onWrite)。cloud函数获取所有刚刚发布的电话号码,然后检索数据库中所有已验证的电话号码,然后需要交叉引用这两个列表以查找所有匹配项。
这是我的代码...
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;
});
});
Run Code Online (Sandbox Code Playgroud)
由于某种原因,当我firebase functions:log在终端中运行时,日志不完整。这是日志...
为什么每次循环for循环时都不会显示日志?它不应该一直打印i = 0到i = 409吗?为什么总是从393开始?而且功能以“超时”状态结束,这是为什么呢?
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
Run Code Online (Sandbox Code Playgroud)
因此,只需运行firebase functions:log -n 500即可获得500条线。他们没有指定是前500个还是后500个(可能最后一个)。但是它足够了。
| 归档时间: |
|
| 查看次数: |
2702 次 |
| 最近记录: |