1
I am having problems to popular a select with the data I have as return of an API. I am using Vue.
Follows codes:
My HTML is like this:
<select>
<option value="" disabled selected>Escolha uma conta</option>
<option v-for="account in accounts" :key="account" :value="account">{{ account }}</option>
</select>
I’m doing the API flame with Aces:
data() {
accounts: []
},
methods: {
async getAccounts() {
await this.$axios.$get('http://localhost:4000/api/accounts')
.then(res => {
// console.log(res)
res.map(account => {
this.accounts.push(account)
// console.log(this.accounts)
})
})
.catch(err => {
// console.log(err)
M.toast({html: 'Houve um erro ao buscar contas.', classes: 'rounded red darken-1', outDuration: 1000})
})
}
},
created() {
this.getAccounts()
}
But when you load the page select is not populated. If I take the comment from the console.log(this.Accounts) after the push, it shows on the console the values inside the right array.
Could someone help me?
#### EDITUpdating the code after a few changes, both in Vue and in the API I consume. And giving some explanations regarding some points that @guastallaigor raised.
First questions from @guastallaigor’s reply:
1 - data () sempre deve ser uma função e ela deve retornar um objeto
R: O código esta com o return no data, é que no momento de fazer a
pergunta eu não selecionei tudo, dei ctrl+c e ctrl+v, eu fui
pegando por parte do código e jogando aqui, ai nessa posso ter
deixado passar o return.
2 - Verifique se a maneira que você está utilizando o axios está
correta, pois está divergente da documentação e da maneira que
costumo utilizar/ver. (axios.get - no meu esta $axios.$get)
R: Estou utilizando desta forma, porque não estou utilizando o
VueJS puro, estou utilizando o Nuxt, que é um "framework" (não
sei se da pra chama-lo assim), construído em cima do Vue, é bem
interessante, e nele ja vem o axios configurado. Aí no Nuxt pra
usar o axios devo dar um this.$axios.$get por exemplo, ai não
necessito dar import no axios também.
3/4 - Verifique se a resposta da API é de fato um array / Verifique se
a API está correta e voltando o JSON da maneira esperada;
R: A API esta retornando um array de objetos da forma que preciso
no front, agora não necessito mais do map ou forEach. Depois
que alterei o retorno da API agora só preciso do
this.accounts = res, e ele ja me retorna corretamente, verifico
isso pelo console.log(res)
5 - Utilize o :key com uma chave única, ou a chave do próprio v-for
R: A API me retorna um array de objetos, com, conta e
descricao_conta. Dentro das options, devo mostrar "conta -
descricao_conta", então como tenho o código da conta, no :key,
eu jogo accounts.conta, e não necessito da chave do próprio
objeto.
Now I will put as this the code of my front:
<select v-model="selected">
<option value="" disabled selected>Escolha uma conta</option>
<option
v-for="account in accounts"
:key="account.conta"
:value="account.conta">
{{ account.conta }} - {{ account.descricao_conta }}
</option>
</select>
data() {
return {
selected: null,
accounts: []
}
}
methods: {
async getAccounts() {
let self = this
await this.$axios.$get('http://localhost:4000/api/accounts')
.then(res => {
// console.log(res)
self.accounts = res
// console.log(self.accounts)
})
.catch(err => {
// console.log(err)
M.toast({html: 'Houve um erro ao buscar as contas.', classes: 'rounded red darken-1', outDuration: 1000})
})
}
}
async created() {
await this.getAccounts()
console.log(this.accounts)
}
The.log console I placed inside created(), it returns the Accounts variable filled with the API data correctly, but the select remains empty. That is, when I call the function that searches the data in the API, it actually populates the variable Accounts, but does not populate the select.
how are
console.log(this.accounts)before thethis.accounts.push(account)?[]orundefined?– Adriano Resende
It returns no data. Returns [ob: Observer]
– Gabriel
I changed the API, and I’ve already set the data to come in Array the way I need, so in Vue I’m just assigning the res to Accounts
– Gabriel
But it still doesn’t work. I tried to create a non-Axios variable to store this, but still it wasn’t
– Gabriel
Why don’t you put it all in
created()? leaving soasync created ()– Adriano Resende
I tried that way but it didn’t work either :/
– Gabriel
I don’t know where it could be wrong
– Gabriel
Inside the getAccounts function instead of setting the value to the Accounts variable, I put a Return res. And within date, where I declare the accoounts variable to assign it by default the method like this: date: { Accounts: getAccounts } But it still doesn’t work
– Gabriel
this getAccounts()
– Gabriel
and so
this.accounts = res?– Adriano Resende
Just as I’m trying
– Gabriel
{{ accounts }}shows the result ofres?– Adriano Resende
If I give a console.log(this.Accounts) within the getAccounts() method it returns the correct values, but if I give console.log(this.Accounts) in created(), after calling this.getAccounts(), it returns empty
– Gabriel
Yeah, that’s what I figured but try those codes inside the
async created(), withoutmethods– Adriano Resende
I tried this way and he doesn’t even bring the console.
– Gabriel
Is the API response correct? The
console.log(res)shows that the data is coming back in the expected way?– guastallaigor
@Gabriel Here are the points you have to test,
thisis working within theasync? if it does not work, nor will it record the data because there is no communication withdata()– Adriano Resende
What if you put
console.log(typeof res, res);where you have// console.log(res)?– Sergio