dimanche 25 avril 2021

Why is this condition not being checked?

I am cuurently working on a small project where I want to check via the google calendar API if a room is busy or not. For that I use this function:

//checking for change of all values. Then console.log values on change and executing request if busy.
function avalabilityCheck() {
    [...inputs].forEach(input => {
            input.addEventListener('change', function () {
                    if (date.value !== "" && startTime.value !== "" && endTime.value !== ""
                    ) {let isBusy = true;
                    //looping through all rooms in compartment
                        for (let key in comp_1) {
                            if (comp_1.hasOwnProperty(key)) {
                                let calendarID = comp_1[key];
                                let roomName = key;
                                //console.log(value);
                                //user input that goes into the freebusy query
                                let requestBody = {
                                    timeMin: date.value + "T" + startTime.value + ":00.000Z",
                                    timeMax: date.value + "T" + endTime.value + ":00.000Z",
                                    items: [
                                        {
                                            id: calendarID
                                        }
                                    ],
                                    timeZone: "GMT+01:00"
                                };


                                //make request to gcalendar if rooms are free. Giving back array on what times room is busy.
                                var freeRequest = gapi.client.calendar.freebusy.query(requestBody);

                                //executing request.
                                freeRequest.execute(function (resp) {
                                    var responseObject = JSON.stringify(resp);
                                    console.log(responseObject);
                                    if (resp.calendars[calendarID].busy.length < 1) {
                                        console.log(`${roomName} is free`);
                                    } else { isBusy = false;
                                    console.log("room is Busy");}

                                })

                          }
                        }

                    console.log("finito");
                    if (isBusy === false) {
                        console.log("working?");
                    }
                    else{console.log("not working");}
                    } else {
                        console.log("change date pls");
                    }
                }
            )
        }
    )
}

now let isBusy = true; and when a room is busy I want isBusy to be false:

if (resp.calendars[calendarID].busy.length < 1) {
                                        console.log(`${roomName} is free`);
                                    } else { isBusy = false;
                                    console.log("room is Busy");} 

So When I run the app the console should either give :

console.log("finito");
if (isBusy === false) {
console.log("working?");
 }
else{console.log("not working");}

But it only gives "finito" to the console and apparently is not checking the state of isBusy. Does anyone see what is the Problem here? Thank you!

Aucun commentaire:

Enregistrer un commentaire