1

I am building a web app using laravel and vuejs. I have made a axios get request to get a list of users . I am getting a Promise object, and from what i have read. Reason for getting a promise object is because it's an async request.

I have tried .then() to get data part of the response. But i am getting a huge script instead of desired data.

axios......then(function(response){
    console.log(response.data);
})

Initially what i did was

 var res = axios.get('/allUsers');
   console.log(res)

That time i came to know about promise object and read about. When i checked network in dev tools, status code is 200 and i can see list of users. So i guess my request is successfully completed.

What should be done to get the list of the users. That list i will be using to update my UI.

1 Answer 1

1

Depending on what you're getting back for data there are a few ways to handle this. You may need to convert the data after the you get receive the response.

axios.get('some_url')
    .then(res => res.json())
    .then(data => {
        // do something with the data
    }).catch(err) {
        conosole.error(err);
    }  

if you're seeing the data come through properly in the response and you're getting what you need without doing that then just do

axios.get('some url').then(res => {
  // do something in here with the data here
})

also make sure you're getting back json if that's what you're looking for. check your response to see if its html or json because they can be handled a bit differently

as an "Edit" you could also handle this with async await so you dont end up in callback hell

async function fetchData() {
    try {
      const res = await axios.get('some url');
      // next step might not be necessary
      const data = await res.json();
      // do something with the data
      console.log(data); // if converting it was necessary
      console.log(res); // if not converting
    } catch (err) {
      console.error(err);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.