Javascript 将firebase数据推送到阵列内+;js+;vuejs
我在Javascript 将firebase数据推送到阵列内+;js+;vuejs,javascript,firebase,vue.js,vuejs2,google-cloud-firestore,Javascript,Firebase,Vue.js,Vuejs2,Google Cloud Firestore,我在firebase-firestore中创建了一个名为users的集合,该集合使用具有唯一id的文档创建 现在我想把它们放入数组中 (在usersCollection中,有3个用户与currentUser.uid一起存储) 例如: fb.usersCollection.where("state", "==", 'online').get().then(querySnapshot => { querySnapshot.forEach((doc) => {
firebase-firestore
中创建了一个名为users的集合,该集合使用具有唯一id的文档创建
现在我想把它们放入数组中
(在usersCollection中,有3个用户与currentUser.uid一起存储)
例如:
fb.usersCollection.where("state", "==", 'online').get().then(querySnapshot => {
querySnapshot.forEach((doc) => {
const userName = doc.data().name
this.markerMy = { name: userName }
})
// push userName inside randomArray
const randomArray = []
randomArray.push(this.markerMy)
我得到它只是为了在数组中推送一个用户,而不是更多用户。您应该在fb.usersCollection
之前声明randomArray
,并在回调中调用推送操作,如下所示:
const randomArray = []
fb.usersCollection.where("state", "==", 'online').get().then(querySnapshot => {
querySnapshot.forEach((doc) => {
const userName = doc.data().name
this.markerMy = {
name: userName
}
randomArray.push(this.markerMy)
})
});