I am trying to iterate over an array of objects to mutate a property. I am using Angular and when I run the ngOnChanges for the first time the functions runs but on the second iteration ( 60 seconds later) the array length is showing as undefined and reporting "Cannot read property length of undefined" even though it was literally defined a minute ago. Can someone please explain wtf is happening here
Code follows
ngOnChanges():void
{
console.log(this.dealership)
this.service.getActiveOffers(this.dealership.dealerid)
.subscribe((response) =>
{
console.log(response)
this.activeQuotes = response;
if(this.activeQuotes.length > 0)
{
this.checkTime()
}
setInterval(this.checkTime,60000)
}, (err) =>
{
console.log(err)
})
}
getExpHours(expiryDate)
{
let now = new Date();
let exp = new Date(expiryDate)
var timeDiff = (exp.getTime() - now.getTime()) / 1000;
timeDiff /= 60;
let hours = timeDiff/ 60;
return hours.toFixed(0)
}
getExpMin(expiryDate)
{
let now = new Date();
let exp = new Date(expiryDate)
var timeDiff = (exp.getTime() - now.getTime()) / 1000;
timeDiff /= 60;
let mins = timeDiff/ 60 / 60;
return mins.toFixed(2).split(".")[1]
}
checkTime()
{
console.log(this.activeQuotes.length)
for(let i = 0; i < this.activeQuotes.length; i++)
{
this.activeQuotes[i].expHours = this.getExpHours(this.activeQuotes[i].expiry)
this.activeQuotes[i].expMins = this.getExpMin(this.activeQuotes[i].expiry)
}
console.log(this.activeQuotes)
}
Value of Array Objects
Array(2)
0:
accepted: false
color: "white"
dealerid: 1
expHours: "21"
expMins: "35"
expiry: "2020-06-24T19:12:48.152Z"
inqid: 2
make: "honda"
model: "accord"
offerid: 2
prodyear: 2020
quoteid: 2
totalvalue: "$19,000.00"
trimtype: "LX"
vehicleid: 31
1:
accepted: false
color: "blue"
dealerid: 1
expHours: "24"
expMins: "39"
expiry: "2020-06-24T22:03:57.210Z"
inqid: 1
make: "honda"
model: "civic"
offerid: 3
prodyear: 2019
quoteid: 3
totalvalue: "$2,000.00"
trimtype: "LX"
vehicleid: 26
Thank you.