This is related to my previous post regarding angular null values.
I am trying to understand why my application / code would behave differently in current situation.
calculateAge(dateOne: Date, dateTwo: Date): number {
let firstDate = new Date(dateOne);
let secondDate = new Date(dateTwo);
let differenceInDates = Math.abs(firstDate.getTime() - secondDate.getTime());
let age = Math.floor(differenceInDates / (1000 * 3600 * 24));
console.log(age);
return age;
}
getFlowers() {
this.flowerSubscription = this.flowerService.getFlowersByFlowerId(this.flowerId, this.someName).subscribe(
res => {
this.dataToSend = res.DataToSend
this.flowerSubscription = this.flowerService.getFlowers(this.dataToSend, this.someName).subscribe(
res => {
this.flower = res; // this.Flower is interface IFlower
console.log(this.flower); <--- this is null
if (this.flower !== undefined) {
this.flowerNumber = (this.flower.FNumber || '--').toString();
this.flowerInDate = this.flower.FlowerInDate;
this.flowerOutDate = this.flower.FlowerInDate;
this.flowerAge = this.calculateAge(this.flower.flowerWarehouseDate, this.flower.flowerStoreDate);
this.flowerStatus = (this.flower.FlowerStatus || '--');
this.customerAmount = (this.flower.CustomerAmount || '--').toString();
this.warehouseAmount = (this.flower.WarehouseAmount || '--').toString();
}
}
}
}
HTML
<table id="FlowerDetails" class="f-fs">
<tr>
<td class="f-title">Flower Nmber</td>
<td class="f-text">{{flowerNumber}}</td>
</tr>
<tr>
<td class="f-title">flowerInDate</td>
<td class="f-text">{{(flowerInDate != null) ? (claimStartDate | date: 'shortDate') : '--'}}</td>
</tr>
<tr>
<td class="f-title">flowerOutDate</td>
<td class="f-text">{{(flowerOutDate != null) ? (flowerOutDate | date: 'shortDate') : '--'}}</td>
</tr>
<tr>
<td class="f-title">Age</td>
<td class="f-text">{{flowerAge}}</td>
</tr>
<tr>
<td class="f-title">Flower Status</td>
<td class="f-text">{{flowerStatus}}</td>
</tr>
</table>
this.flower
on printing in console is null. I am setting the null values to --
if the data is null. However, console still shows a null and --
are not displayed as expected. I am using API calls to get data from backend. I dont understand why the code behaves differently when everything is same.