I'm trying to access an element in an array and it returns undefined every time. I've created the array using .push(). The length of the array also displays as 0.
orgHolidayYears = new Array();
const orgPublicHolidays = new Array();
const organisationID = Knack.getUserAttributes().values.field_382[0];
$.ajax({
url: "https://api.knack.com/v1/objects/object_26/records",
headers:
success: function(data) {
// filters working public holidays their organisation
for (let i in data.records) {
// does the organisation in the public holiday correspond to the organisation of the user
if (data.records[i].field_384_raw[0].id === organisationID) {
// add the public holiday date to the array
orgPublicHolidays.push(data.records[i].field_221);
}
}
return orgPublicHolidays;
}
});
// pulls in holiday years
$.ajax({
url:
headers: {
success: function(data) {
// filters working holiday years their organisation
for (let i in data.records) {
// does the organisation in the public holiday correspond to the organisation of the user
if (data.records[i].field_385_raw[0].id === organisationID) {
// add the public holiday date to the array
const holYearData = {
startDate: data.records[i].field_268,
endDate: data.records[i].field_269,
};
orgHolidayYears.push(holYearData);
}
}
return orgHolidayYears;
}
});
console.log(orgHolidayYears);
console.log(orgHolidayYears[0]);
Image of console