Forum Discussion

iftomkins's avatar
11 years ago

How to handle an error when page.context.user. does not exist

I'm getting an error on View My Profile page, which is likely coming from this code: <#if user.id == page.context.user.id>

 

My guess is that page.context.user.id or user.id doesn't exist, so it throws an error. How do I handle that error in a conditional, so that instead of breaking, it just returns false, such as:

 

     <#if user.id?? && page.context.user.id??>
         <#if user.id == page.context.user.id> <#-- Only show Kudos tab on your own profile page -->
           //stuff here
         </#if>
     </#if>

 

Thanks! Here's the whole code:

 

<#-- Add links -->
<ul class="profile-tabs">

   <li class="tab-activity">
      <a href="/t5/user/viewprofilepage/user-id/${page.context.user.id}/tab/activity">${text.format('general.Posts')}</a>
   </li>
     <#if user.id?? && page.context.user.id??>
         <#if user.id == page.context.user.id> <#-- Only show Kudos tab on your own profile page -->
           <li class="tab-votes">
              <a href="/t5/kudos/mobilemykudospage">${text.format('text.UserRankNodeEditor.kudos.title')}</a>
           </li>
         </#if>
     </#if>
   <li class="tab-bio">
      <a href="/t5/user/viewprofilepage/user-id/${page.context.user.id}/tab/bio">${text.format('taplet.commonComponents.nodeDescription.title')}</a>
   </li>

</ul>

  • Can you try:
    page.context.user?? && page.context.user.id??
  • NicoB's avatar
    NicoB
    Lithium Alumni (Retired)
    Can you try:
    page.context.user?? && page.context.user.id??
    • iftomkins's avatar
      iftomkins
      Maven

      Perfect! That worked, thanks. The main issue was that when you're on the mobilemykudospage, any use of page.context.user.id throws an error. The below code (included below), avoids that.

       

      Screenshot of tabs on user's own profile page: http://screencast.com/t/pFPw7URGR

      Screenshot of tabs on someone else's profile page: http://screencast.com/t/AuxUgKJ3


      <#-- Displays profile tabs: posts, kudos, and bio. If a user is viewing someone else`s profile page, the Kudos tab is hidden. -->
      <ul class="profile-tabs">
           <#if page.context.user?? && page.context.user.id?? >
                    <li class="tab-activity">
                        <a href="/t5/user/viewprofilepage/user-id/${page.context.user.id}/tab/activity">${text.format('general.Posts')}</a>
                    </li>
                    <#if user.id == page.context.user.id> <#-- Only show Kudos tab on your own profile page -->
                        <li class="tab-votes">
                        <a href="/t5/kudos/mobilemykudospage">${text.format('text.UserRankNodeEditor.kudos.title')}</a>
                      </li>
                  </#if>
                  <li class="tab-bio">
                        <a href="/t5/user/viewprofilepage/user-id/${page.context.user.id}/tab/bio">${text.format('taplet.commonComponents.nodeDescription.title')}</a>
                    </li>
           <#else>
               <#if page.name?lower_case == "mobilemykudospage" >    
                     <li class="tab-activity">
                        <a href="/t5/user/viewprofilepage/user-id/${user.id}/tab/activity ">${text.format('general.Posts')}</a>
                     </li>
                     <li class="tab-votes">
                        <a href="/t5/kudos/mobilemykudospage ">${text.format('text.UserRankNodeEditor.kudos.title')}</a>
                     </li>
                     <li class="tab-bio">
                        <a href="/t5/user/viewprofilepage/user-id/${user.id}/tab/bio">${text.format('taplet.commonComponents.nodeDescription.title')}</a>
                     </li>
               </#if>
           </#if>
      </ul>