Home > Mobile >  Vue Js While inside the function I called with Axios get, printing the incoming data into the array
Vue Js While inside the function I called with Axios get, printing the incoming data into the array

Time:12-23

Inside the function in the screenshot, I am adding the data from the backend function to the array with axios.get. But when I go outside of axios, the values of the array I print are undefined.

I am getting string value from backend. I want to be able to use it in different methods by returning it. Please help me. I can't find the solution.

getReasonsForWaitingCustomer() {
  this.adimKodlariLastString = "";
  if (this.$route.params.status == "musteri-bekleniyor") {
            axios.get(URL   "xxx/xxx?xxx=xxx)
                .then(response => {
                    for (var i = 0; i < response.data.data.length; i  ) {
                        if (this.stepCode.includes(response.data.data[i].adim_kodu) == false) {
                            this.stepCode.push(response.data.data[i].adim_kodu);
                        }
                    }
                    for (var j = 0; j < this.stepCode.length; j  ) {
                            this.adimKodlari  = this.stepCode[j]   ",";
                        }
                    this.adimKodlariLastString = this.adimKodlari.slice(0, -1);
                    console.log("inAxiosThen",this.adimKodlariLastString);
                })
        }
        console.log("afterAxios",this.adimKodlariLastString);
return "apfapofapkapfka" --> It's working
return this.adimKodlariLastString --> It's not working. I want this to work.
},

In the solution examples I reviewed, the incoming value was used in the html tags. But I want to be able to use incoming value in methods.

When the string values I want to use are in .then(response), I can get them when I press the console.

When I press the console other than .then() I don't get the values.

CodePudding user response:

You won't get any data this way because axios calls are asynchronous. The easiest and cleaner way is:

const response = await axios.get('url')
console.log(response.data)
this.adimKodlariLastString = response.data
// and then you could do your logic

Alternatively, to have it working with your code, you could just add:

await

before axios (you could do it with pure Promises but you'll end up getting Promise inside Promise).

CodePudding user response:

Current,this is my function's last status. This is working. Return is successfull.

async getReasonsForWaitingCustomer() {
  let adimKodlariLastString = "";
  let stepCode = [];
  let adimKodlari = "";
  if (this.$route.params.status == "xxx") {
            const response = await axios.get(URL   "xxx/xxx");
                    for (var i = 0; i < response.data.data.length; i  ) {
                        if (stepCode.includes(response.data.data[i].adim_kodu) == false) {
                            stepCode.push(response.data.data[i].adim_kodu);
                        }
                    }
                    for (var j = 0; j < stepCode.length; j  ) {
                            adimKodlari  = stepCode[j]   ",";
                        }
                    adimKodlariLastString = adimKodlari.slice(0, -1);
        }
        console.log("adimKodu",adimKodlariLastString);
      return adimKodlariLastString;
},

Promise in console

I looked at their solutions, but I didn't understand anything. In the solutions they manually assigned a value and called the result. I have no idea how to get my incoming data in PromiseResult.

  • Related