Axios request question: can't you get what is written by the then method? Or how to write the data returned by response?

when I am developing, I will directly

export let getSliders = ()=>{
  return axios.get("/sliders");
};

you can return the data to the component, but

export let getSliders = ()=>{
axios.get("/sliders").then(response=>{
    console.log(response);//
  }).catch((err)=>{
    console.log(err);
  })

};

this is how it is called in the component

 async getSlider(){
          this.sliders= await getSliders();
        },

how can you not get it when you write it later with the then method? Or how to write the data returned by response? Ask for guidance

Mar.05,2021

export let getSliders = ()=>{
  return axios.get('/sliders');
};

just use this again, and there is also a promise after await.


if you use await, shouldn't your data already be in this.sliders
if you want to use then, then go directly

 async getSlider(){
    return await getSliders();
 }

as to why you can't get the data, because your async function does not return a value, it will resolve a undefined as the value, of the fulfilled status of the promise, so you can definitely not get the data. The data is already in the this.sliders
https://codeshelper.com/a/11.. If you need to read this article, you can learn about promise

.
Menu