I have a vue.js app made with single file components. I have an empty array declared in my data:
data: function () {
return {
teamKeys: []
}
},
Then I push values into it in one of my methods:
fetchTeams (token) {
var this_ = this
var url = 'myapiserver.com/teams'
this.axios.get(url, {
params: {
accessToken: token
}
})
.then(function (response) {
var teams = response.data.teams[0].teams
teams.forEach(function (t) {
this_.teamKeys.push(String(t.team_key))
})
})
.catch(function (error) {
console.log(error)
})
},
(updated with actual fetchTeams method) Then, in another method, I try to loop through it using forEach.
fetchRosters (token) {
var teamKeys = this.teamKeys
teamKeys.forEach(function (key) {
console.log(key)
// do stuff
})
}
The methods are called one after the other in mounted:
mounted () {
this.$nextTick(() => {
var accessToken = this.$route.query.accessToken
this.fetchTeams(accessToken)
this.fetchRosters(accessToken)
})
}
The forEach loop doesn't work for some reason, it seems like the array is being treated as empty because the inside of the forEach never gets accessed. Also, if I do this just before calling forEach:
console.log(this.teamKeys)
console.log(JSON.stringify(this.teamKeys))
the second output is blank [], and the first one is output in console like so:
Array(2)
0: "371.l.215756.t.1"
1: "371.l.621475.t.2"
length: 2
__ob__: Observer {value: Array(2), dep: Dep, vmCount: 0}
__proto__: Array
I'm sure I'm doing something stupid. Am I not creating the array properly? Do I even know what an array is?
console.log(this.teamKeys.length)console.log(this.teamKeys)just before, it shows up withlength: 2in the console.