it passes the object to the function but I don't know why, despite the fact that it is visible there, I cannot return individual elements of this object because it shows "undefined" in the console logs. Anyone have any idea what I should do or am I doing wrong?
Code:
async function getMonitors(){
try {
let res = await fetch(API_URL "/" API_VERSION "/status/monitors/" location.hostname "/list");
var data = await res.json();
if(data.success){
delete data.success;
return data;
}else{
e=data.message;
}
} catch(error){
e=error;
}
if(typeof e!="undefined"&&e!==null){system.error(e);}
}
function getMonitorsData(data){
let totalMonitors = data.totalMonitors;
console.log(data);
}
var m = getMonitors();
var data = getMonitorsData(m);
Screen of the returned object in the function Object
CodePudding user response:
you have forgotten to call the function and pass the data that are response from your api
async function getMonitors(){
try {
let res = await fetch("https://www.7timer.info/bin/astro.php?lon=113.2&lat=23.1&ac=0&unit=metric&output=json&tzshift=0");
var data = await res.json();
getMonitorsData(data) // here is what i added for passing response to function
if(data.success){
delete data.success;
return data;
}else{
e=data.message;
}
} catch(error){
e=error;
}
if(typeof e!="undefined"&&e!==null){system.error(e);}
}
function getMonitorsData(data){
let totalMonitors = data.totalMonitors;
console.log(data);
}
var m = getMonitors();
var data = getMonitorsData(m);
CodePudding user response:
You have missed await
keyword while calling getMonitors()
function.
async function getMonitors(){
try {
let res = await fetch(API_URL "/" API_VERSION "/status/monitors/" location.hostname "/list");
var data = await res.json();
if(data.success){
delete data.success;
return data;
}else{
e=data.message;
}
} catch(error){
e=error;
}
if(typeof e!="undefined"&&e!==null){system.error(e);}
}
function getMonitorsData(data){
let totalMonitors = data.totalMonitors;
console.log(data);
}
async function init() {
var m = await getMonitors();
var data = getMonitorsData(m);
}
init();