Javascript 如何获取此数组的所有后代?
我正在尝试获取所有用户的Javascript 如何获取此数组的所有后代?,javascript,firebase,vue.js,for-loop,firebase-realtime-database,Javascript,Firebase,Vue.js,For Loop,Firebase Realtime Database,我正在尝试获取所有用户的voornamen和lidnumers,我将它们添加到firebase数据库我需要一个列表来选择您想要玩的用户,这就是我尝试的: //this is my database store where I get the users const gebruikersref = firebase.database().ref('Gebruikers/') self = this gebruikersref.once('value', functi
voornamen
和lidnumers
,我将它们添加到firebase数据库我需要一个
列表来选择您想要玩的用户,这就是我尝试的:
//this is my database store where I get the users
const gebruikersref = firebase.database().ref('Gebruikers/')
self = this
gebruikersref.once('value', function(snapshot){
//inside Gebruikers where all users are stored
var voornamen = ''
var lidnummers = ''
var childlength = snapshot.numChildren()
snapshot.forEach(function(childSnapshot){
//everything that is stored inside the user
const data = childSnapshot.exportVal();
self.voornamen = data.Voornaam
self.lidnummers = data.Lidnummer
});
//loop through the length of gebruikers and set medespelers
for(let i=0; i<childlength;i++) {
self.medespelers = self.voornamen + " - " + self.lidnummers;
}
});
我的问题是,它只返回最后一个用户,因为foreach循环首先在
lidnumers和voornamen
中设置所有用户,当最后一个用户设置为for循环运行时,这意味着它只调用最后一个用户X gebruikers中的childitems。我还尝试将for循环嵌套在foreach循环中,但这不起作用,因为它将返回所有用户X gebruikers中的用户。有人知道我如何解决我的问题并让所有用户都使用x1吗?看来第二个循环是不必要的。试试这个:
const medespellers=[];//新的空数组
snapshot.forEach(函数(childSnapshot){
//存储在用户内部的所有内容
const data=childSnapshot.exportVal();
self.voornamen=data.Voornaam
self.lidnumers=data.lidnumer
medespelers.push(self.voornamen+“-”+self.lidnumers);//添加到数组中
});
self.medespelers=medespelers;//将数组设置为数据变量
您还应该使用as回调,以避免使用
self
约定。第二个循环似乎是不必要的。试试这个:
const medespellers=[];//新的空数组
snapshot.forEach(函数(childSnapshot){
//存储在用户内部的所有内容
const data=childSnapshot.exportVal();
self.voornamen=data.Voornaam
self.lidnumers=data.lidnumer
medespelers.push(self.voornamen+“-”+self.lidnumers);//添加到数组中
});
self.medespelers=medespelers;//将数组设置为数据变量
您还应该使用as回调来避免使用self
约定
<select class="banen rounded">
<option v-for="(option, index) in medespelers" :key="index">{{ medespelers }}</option>
</select>