• Jobs
  • About Us
  • Jobs
    • Home
    • Jobs
    • Courses and challenges
  • Businesses
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

218
Views
Vue Js While inside the function I called with Axios get, printing the incoming data into the array

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.

over 3 years ago · Santiago Trujillo
3 answers
Answer question

0

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).

over 3 years ago · Santiago Trujillo Report

0

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.

over 3 years ago · Santiago Trujillo Report

0

Good news. I founded solution of this problem. I hope it will be useful for those who are looking for a solution. As I mentioned, I said that I wanted to use the value that came as a prompt in another function.

I'm successfully called my values in this function

I'm converted this function the async. I brought the first function as await.

Last result in the console

over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Show me some job opportunities
There's an error!