Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

302
Visualizações
inconsistent respond from data and API using vue.js and axios

I'm trying to load data retrieved from my API using axios into vue.js data function. i've split the process to two functions, one retrieving the data from my api and assigning it to a variable and another one that should manipulate it.

unfortunately when i log the data in each function i get different results.

here is what im doing:

let sendGetRequest = async () => {
  try {
    console.log('getting');
    const res = await axios.get('/api/v1/leaderboard');
    let resData= res.data
    this.pastWinners = resData.history
    this.chart = resData.chart
    console.log('this ', this.chart)
    this.users = resData.users
  } catch (err) {
    console.error(err)
  }
};

let objectManipulation = () => {
  console.log('manipulating');
  console.log(this.chart)
}

sendGetRequest();
objectManipulation();

the first log (from sendGetRequest) return the data as it should be:

(7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, __ob__: Observer]

whilst the second function returns:

[__ob__: Observer]
length: 0
__ob__: Observer {value: Array(0), dep: Dep, vmCount: 0}
[[Prototype]]: Array

thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Thanks to @daantje answer i was able to fix the problem.

to resolve it i called sendGetRequest from within objectManipulation using async & await, like this:

let sendGetRequest = async () => {
  try {
    console.log('getting');
    const res = await axios.get('/api/v1/leaderboard');
    let resData= res.data
    this.pastWinners = resData.history
    this.chart = resData.chart
    console.log('this ', this.chart)
    this.users = resData.users
    console.log('done')
  } catch (err) {
    console.error(err)
  }
};

let objectManipulation = async () => {
  // eslint-disable-next-line no-unused-vars
  let res = await sendGetRequest()
  console.log('manipulating');
  console.log(this.chart)
}

objectManipulation();
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda