undefined passes if?

Matt SonesMatt Sones 💎💎💎
edited February 10, 2017 in Questions
Here's part of some code for a custom table view:
.attr('href', '/support/servlet/servlet.FileDownload?file=' +                (row.Support_User__r && row.Support_User__r.Photo__r) ? 
       row.Support_User__r.Photo__r.skuid__AttachmentId__c : 
       null
)
I'm getting a javascript error on row.Support_User__r.Photo__r.skuid__AttachmentID__c:
Uncaught TypeError: Cannot read property 'skuid__AttachmentId__c' of undefined
How could the code get past the IF ? when Photo__r is undefined?

Any idea what might be going on here?

Comments

  • edited January 12, 2017
    That's because of precedence of the operators.

    ? is considered more important than +

    Your statement 
    '/support/servlet/servlet.FileDownload?file=' +                (row.Support_User__r && row.Support_User__r.Photo__r)  ?  row.Support_User__r.Photo__r.skuid__AttachmentId__c :  null
    is interpreted as
    ('/support/servlet/servlet.FileDownload?file=' + (row.Support_User__r && row.Support_User__r.Photo__r))  ?  (row.Support_User__r.Photo__r.skuid__AttachmentId__c) : (null)
     try putting the whole inline-if into brackets like
     '/support/servlet/servlet.FileDownload?file=' + ((row.Support_User__r && row.Support_User__r.Photo__r)  ?  row.Support_User__r.Photo__r.skuid__AttachmentId__c :  null)
  • Matt SonesMatt Sones 💎💎💎
    edited February 10, 2017
    Thanks, Thimo!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!