0

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.

17
  • if (variable) is equivalent to if (variable!=undefined && variable!=null && variable!=0 && variable!="") and if (!variable) is equivalent to if (variable==undefined || variable==null || variable==0 || variable="") Commented Jun 26, 2018 at 16:46
  • @Eliseo, shouldnt || handle the null values? Commented Jun 26, 2018 at 16:50
  • Do you mean that console.log(this.flower); <--- this is null is not what you want? Should console.log(this.flower) print --? Commented Jun 26, 2018 at 16:51
  • If this.flower is null, then this.flower.FNumber would throw an error. Are you getting any errors? Commented Jun 26, 2018 at 16:57
  • @user184994, I get only null exception. No errors otherwise. Commented Jun 26, 2018 at 17:03

2 Answers 2

0

Maddy

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){ //if this.flower <> undefined and <>null
              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();
            }
            else
            {
              this.flowerNumber = '--';
              this.flowerInDate = '--';
              this.flowerOutDate = '--';
              this.flowerAge = 0;
              this.flowerStatus = '--';
              this.customerAmount = '--';
              this.warehouseAmount = '--';
            }
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Eliseo, maybe I dont understand it fully and if you could help me explain. In my if statement, the ` || ` should validate null. Isnt that right?
Also, what if only one thing is null in the if condition. I think this will fail if that is the case.
@Maddy, this.flower can be null or not. If this.flower is not null, this.flower.FlowerStatus can be null. I think that this.flowerStatus=this.flower.FlowerStatus || '--' must be ok. The other statement that say Hyuc Kang this.flowerStatus=this.flower.FlowerStatus? this.flower.FlowerStatus:'--' is ok too (is the ternary operator). But the two last statement give you an error is this.flower is null
0

I found a solution by checking for this.dataToSend for null. If its null, I set all the values to --, otherwise I set the values with actual variables. @Eliseo, Thanks for the help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.