0

I tried to store the value of array from JSON to a global normal array.

When I do console.log inside the block of .then of axios, I can get the value inside the global normal array (it's named "kunci")

first try inside axios

const axios = require("axios");

let kunci = [];

    axios.get("http://localhost:5000/lelang/get_kriteria").then(hasil =>{
        let coba = [];
        for(let i=0; i < hasil.data.syarat.length;i++){
            coba[i] = hasil.data.syarat[i].parameter_question;
             kunci.push(coba[i])   
        }
    console.log(kunci)
    })

, and when I put the console.log outside the axios block, it becomes empty

become empty when outside axios

const axios = require("axios");

let kunci = [];

axios.get("http://localhost:5000/lelang/get_kriteria").then(hasil =>{
    let coba = [];
    for(let i=0; i < hasil.data.syarat.length;i++){
        coba[i] = hasil.data.syarat[i].parameter_question;
         kunci.push(coba[i]) 

    }
})
console.log(kunci)

and because I got stuck, I tried too to use async function, but it has the same result with async function

const axios = require("axios");

let kunci = [];


async function getKriteria(){
    try{
        const result = axios.get("http://localhost:5000/lelang/get_kriteria");
        return result; 
    }catch(e){
        console.log(error);
    }
}


getKriteria().then(hasil => {
    let coba = hasil.data.parameter;
    coba.forEach(element => kunci.push(element.parameter_question))
})


console.log(kunci)

and this is what you will get inside url that becomes parameter inside axios.get localhost:5000/lelang/get_kriteria

{
  "parameter": [
    {
      "id_parameter_question": 1,
      "parameter_question": "Administrasi",
      "bobot": 0
    },
    {
      "id_parameter_question": 2,
      "parameter_question": "Peminatan Tower Power",
      "bobot": 0
    },
    {
      "id_parameter_question": 3,
      "parameter_question": "Financial Capability",
      "bobot": 0
    },
    {
      "id_parameter_question": 4,
      "parameter_question": "Pengalaman",
      "bobot": 0
    },
    {
      "id_parameter_question": 5,
      "parameter_question": "Team Availability",
      "bobot": 0
    },
    {
      "id_parameter_question": 6,
      "parameter_question": "Stock Material dan Logistik",
      "bobot": 0
    },
    {
      "id_parameter_question": 7,
      "parameter_question": "Peralatan yang Digunakan",
      "bobot": 0
    }
  ]
}

Is there any mistake that I made? Please help me to return the value parameter_question

Thank you

Makyen
  • 31,849
  • 12
  • 86
  • 121
  • it would be better if you put the code in the question instead of attaching screenshot. It is easy to see and copy the code and try – Ashish Modi Feb 04 '20 at 13:19
  • 1
    Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Nipun Jain Feb 04 '20 at 13:20
  • You ***cannot*** use asynchronous data this way and expect consistent results. You ***have*** to treat asynchronous data as "toxic" in that anything that depends on asynchronous data is itself asynchronous. – zero298 Feb 04 '20 at 15:37

0 Answers0