Chaining conditions using currentUser


#1

I am using the currentUser helper in m template file and i was wondering whether how i can achieve this.

<h5 class="panel-title"> {{#if currentUser.profile.userrole = 'schooladmin' or 'teacher' or 'student' or 'parent' or 'superadmin'}} Academic Years {{/if}}</h5>

I want to chain using if using the or statement.

The above chaining causes this error

Can't have a non-keyword argument
   after a keyword argument

How can i correct this?.


#2

Create a Template helper to handle this. Something like:

some_template.js:

Template.someTemplate.helpers({
  acceptedRole() {
    let acceptedRole = false;
    const acceptedRoles = ['schooladmin', 'teacher', 'student', 'parent', 'superadmin'];
    const user = Meteor.user();
    if (user && (acceptedRoles.indexOf(user.profile.userrole) > -1)) {
      acceptedRole = true;
    }
    return acceptedRole;
  }
});

some_template.html:

<template name="someTemplate">
  ...
  <h5 class="panel-title"> 
    {{#if acceptedRole}} 
      Academic Years
    {{/if}}
  </h5>
  ... 
</template>

#3

I have defined a global helper

Template.registerHelper("custom", function() {
       return (Meteor.user().profile.userrole === 'schooladmin' || 'teacher' ||  'student' || 'parent' || 'superadmin');
});

and used it like so

<h5 class="panel-title"> {{#if custom}} Academic Years{{/if}}</h5>


#4

Hopefully, you’ve discovered the error of your ways, but if not, realize that your function as you’ve shown it should never return “false”. It will either return true if the userrole is ‘schooladmin’ or it will return ‘teacher’. Because ‘teacher’ is truthy, that will act like true when tested. From the console:

> "this" === "that" || "the other" || "thing" || "the other thing"
 "the other"
> if ("this" === "that" || "the other" || "thing" || "the other thing") console.log('yeh sure');
 yeh sure

Something like the following construct might serve your needs…

> ['schooladmin', 'teacher', 'student', 'parent', 'superadmin'].includes('test')
false
> ['schooladmin', 'teacher', 'student', 'parent', 'superadmin'].includes('teacher')
true
... or in your case ...
['schooladmin', 'teacher', 'student', 'parent', 'superadmin'].includes(Meteor.user().profile.userrole)