В настоящее время я пытаюсь получить / прочитать данные, хранящиеся в моей базе данных на 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, где каждый идентификатор пользователя:
{
"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
Я сейчас вывожу все в терминал.