我试图让voornamen
与lidnummers
所有用户的,我将它们添加到数据库的火力点,我需要一个<select>
列表,选择你要玩的用户,这是我的尝试:
//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;
}
});
然后在data()
我返回medespelers: ''
,这是我的html:
<select class="banen rounded">
<option v-for="(option, index) in medespelers" :key="index">{{ medespelers }}</option>
</select>
我的问题是,它仅返回最后一个用户,因为foreach循环首先运行所有用户,lidnummers and voornamen
而设置了最后一个用户则for循环运行,这意味着它仅将最后一个用户X称为gebruikers中的子项。我还尝试过将for循环嵌套在foreach循环中,但这将无法工作,因为它将返回所有用户X和gebruikers中的用户。有人知道我如何解决我的问题并让所有用户获得X 1吗?
看来您的第二个循环是不必要的。尝试这个:
const medespelers = []; // new empty array
snapshot.forEach(function(childSnapshot){
//everything that is stored inside the user
const data = childSnapshot.exportVal();
self.voornamen = data.Voornaam
self.lidnummers = data.Lidnummer
medespelers.push(self.voornamen + " - " + self.lidnummers); // adding to the array
});
self.medespelers = medespelers; // setting your array to the data variable
您还应该使用箭头函数作为回调,以避免需要self
约定。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句