Promise not returning value

Asked

Viewed 452 times

2

My request is returning this to me:

data: Promise

Opening Chrome Developer Tools I see this:

data: Promise
__proto__:Promise
[[PromiseStatus]]:"resolved"
[[PromiseValue]]:"24/01/2016"

Using Axios, how is it possible to print this value ?

  Lista (url) {
    axios.get(url)
    .then(res => res.data)
    .then(res => this.publicacaoList = res.map( (contato) => ({
      data: axios.get(contato.id_url).then(res => res.data.data_registro).catch(e => console.log(e)),
      email: contato.email,
      tel: contato.tel
    }) )   )
    .catch(e => {
      console.log(e)
    })
  }

Where to contact.id_url is a url where I request to find the date. The email table fields and tel return normally, the date field returns an empty object {}

  • If we make a suggestion with async/await you have to compile?

  • Unfortunately I won’t be able to test, I already quit the internship and the api I was consuming I don’t have access at home. But I will continue studying javascript and Monday I will probably be able to solve it. Monday I’ll come back with the answer. Thank you so much for always trying to help me

  • This can be solved with chained Promises, but with async/await is even easier. You’re using webpack?

  • Yes, webpack. I’m really enjoying Vue, I bought an ebook today (The Majesty of Vue.js 2) and I will try to read and practice this weekend, so next week I will be able to do a CRUD.

1 answer

1


When you do data: axios.get(contato.id_url) this var return a file to data, and what you want is the value of that ajax. Then you have to solve that ajax(s) first and then set data.

Suggestion:

Lista(url) {
  axios.get(url)
    .then(res => {
      const futureData = res.data.map(contato => axios.get(contato.id_url));
      return Promise.all(futureData).then(contactos =>
        this.publicacaoList = contactos.map((data, i) => ({
            data: data,
            email: res.data[i].email,
            tel: res.data[i].tel
          });
        });
    });
  .catch(e => {
    console.log(e)
  })
}

  • 1

    Functionouuuu, with the correction date: data.data.data_record .

  • 1

    @mtAlves https://answall.com/q/119907/129

Browser other questions tagged

You are not signed in. Login or sign up in order to post.