0

I am getting an object from backend and that object has several keys and values. According to the database data sometimes some of the keys won't be available. So I have used ternary operator for it but still, it is not working.

 `
  <tr>
     <td>
         ${(df[temp].image.status)?df[temp].image.status:'Not Assigned'}
     </td>
  </tr>
  `

Expected result: Whether an "image" key is there or not there shouldn't be any error. Either it should be some status or 'Not Assigned';

Current result: TypeError: df[temp].image is undefined

Anyone any idea why this is happening?

2
  • 1
    One is image.status and the other says imagestatus. The first one probably has an extra . Commented May 30, 2019 at 11:58
  • Check wether df[temp].image exists at first. Commented May 30, 2019 at 12:01

1 Answer 1

2

You are checking on already undefined value. First you need to verify that image exists and then you can access its fields.

<tr>
    <td>
        ${(df[temp].image && df[temp].image.status) ? (df[temp].image.status) : 'Not Assigned'}
    </td>
</tr>
Sign up to request clarification or add additional context in comments.

4 Comments

so we cant directly check for df[temp].image.status?
@lealceldeiro good point, edited answer to include check also for status field
@swetasharma no, you cannot, whenever you try to access status, you need to have image object present already
you don't need the ternary here df[temp].image && df[temp].image.status || 'Not Assigned' will also work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.