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