我目前正在尝试获取/读取Firebase上数据库中存储的数据。由于某种原因,该请求运行了太多次,我不确定为什么会这样。 (即使在我要写入相同数据的20-30个额外插槽的地方,也会发生这种情况)。 有人可以告诉我为什么会这样吗,我该怎么做? 我的项目基本上是打印出一个Family Tree连接,该连接最终将用于前端。但是我正在处理大量的信息/数据。 我目前仅在Javascript上运行此程序(尚无前端连接)。我正在使用webstorm作为IDE编写此代码。以下是我的代码:
export const displayFam = (user,position) => {
var relate = db.ref().child("Users").child(user).once("value", function(snapshot) {
snapshot.forEach(function(item){
var itemVal = item.val();
if(itemVal.mother != null){
displayFam(user,itemVal.mother);
console.log(itemVal.Firstname + "'s mother is " + itemVal.mother);
}
else{
console.log(itemVal.Firstname + " has no mother.");
}
if(itemVal.father != null){
displayFam(user,itemVal.father);
console.log(itemVal.Firstname + "'s father is " + itemVal.father);
}
else{
console.log(itemVal.Firstname + " has no father.");
}
});
console.log(snapshot.numChildren());
});
} 这是我的Firebase数据库上的当前数据,其中每个ID是一个用户:
{
"K469jg05DnZHqn8yzp1Ucghc12n2" : [ {
"Firstname" : "Jonathan",
"Lastname" : "Smith",
"father" : 1,
"gender" : "male",
"mother" : 2
}, {
"Firstname" : "Patrick",
"Lastname" : "Smith",
"gender" : "male",
"mother" : 3
}, {
"Firstname" : "Lisa",
"Lastname" : "Smith",
"gender" : "female"
}, {
"Firstname" : "Amelia",
"Lastname" : "Smith",
"gender" : "female"
} ]
}
这是我输出的一部分:
Patrick's mother is 3
Patrick has no father.
Lisa has no mother.
Lisa has no father.
Amelia has no mother.
Amelia has no father.
Jonathan's mother is 2
Jonathan's father is 1
Patrick's mother is 3
Patrick has no father.
Lisa has no mother.
Lisa has no father.
Amelia has no mother.
Amelia has no father.
Jonathan's mother is 2
Jonathan's father is 1
Patrick's mother is 3
Patrick's mother is 3
Patrick has no father.
Lisa has no mother.
Lisa has no father.
Amelia has no mother.
Amelia has no father.
Jonathan's mother is 2
Jonathan's father is 1
Patrick's mother is 3
我希望只有这样:
Lisa has no mother.
Lisa has no father.
Amelia has no mother.
Amelia has no father.
Jonathan's mother is 2
Jonathan's father is 1
Patrick's mother is 3
我目前将所有内容输出到终端。