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
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();
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);
try to console.log(data.totalMonitors) to see if you actually got the right location
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.