JuanIsFree
JuanIsFree

Reputation: 370

User undefined momentarily in Meteor

I set up a template with materialize tabs in meteor with conditional formatting if a user is an admin. Unfortunately, this only works intermittently. Does anyone understand why?

The template:

<template name="tabs">
  <div class="row">
    <div class="col s12">
      <ul id="nav-mobile" class="tabs right darkgrey">
        {{#if currentUser}}
          <li class="tab col s4"><a href="#forms">Forms</a></li>
          <li class="tab col s4"><a href="#report" class="active">Report</a></li>
        {{/if}}
        {{#if isAdmin}}
          <li class="tab col s4"><a href="#edit">Edit</a></li>
        {{/if}}
      </ul>
    </div>
  </div>
  <div class="black-text" id="edit">edit</div>
  <div class="black-text" id="forms">forms</div>
  <div class="black-text" id="report">report</div>
</template>

The js (admin user is logged in):

Template.tabs.helpers({
  isAdmin: function() {
    var u = Meteor.user();
    try {
      if (u == undefined) {
        console.log("user was undefined momentarily?");
        console.log(u);
        console.log(u.username);
      }
    } catch (err) {
      console.log(err);
    }
    return u.username == "admin";
    //return true;
  }
});

I then get some strange output (browser console) + stacktrace:

user was undefined momentarily? forms.js:29:11
undefined forms.js:30:11
TypeError: u is undefined
Stack trace:
.isAdmin@http://localhost:3000/forms.js?bb20f376b815e838347b13b36da9b44e65fad3d9:31:11
bindDataContext/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2880:14
Blaze._wrapCatchingExceptions/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1651:14
wrapHelper/</<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2928:14
Template._withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:12
wrapHelper/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2927:1
Spacebars.call@http://localhost:3000/packages/spacebars.js?7bafbe05ec09b6bbb6a3b276537e4995ab298a2f:172:12
@http://localhost:3000/template.forms.js?70bd177091ff1ea210ad3e4ae259b54f523adf01:42:12
Blaze.If/</<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2626:44
viewAutorun/</<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1865:16
Template._withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:12
viewAutorun/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1864:1
Blaze._withCurrentView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2197:12
viewAutorun@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1863:1
Tracker.Computation.prototype._compute@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:327:5
Tracker.Computation@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:243:5
Tracker.autorun@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:566:11
Blaze.View.prototype.autorun@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1875:14
Blaze.If/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2625:1
fireCallbacks@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1941:9
Tracker.nonreactive@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:593:12
Blaze._fireCallbacks/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1938:5
Blaze._withCurrentView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2197:12
Blaze._fireCallbacks@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1937:3
Blaze._createView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1955:3
Blaze._materializeView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1998:3
materializeDOMInner@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1476:9
Blaze._materializeDOM@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1428:7
Blaze._materializeView/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2040:25
Tracker.nonreactive@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:593:12
Blaze._materializeView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2004:3
Blaze.render@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2296:3
Template.body.renderToDocument@http://localhost:3000/packages/templating.js?376767bb0d2463b3b2615a1b90e77f6b22d39d7b:105:14
ready/runStartupCallbacks@http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:785:8
ready@http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:787:3
 forms.js:34:9

Exception in template helper: .isAdmin@http://localhost:3000/forms.js?bb20f376b815e838347b13b36da9b44e65fad3d9:36:7
bindDataContext/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2880:14
Blaze._wrapCatchingExceptions/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1651:14
wrapHelper/</<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2928:14
Template._withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:12
wrapHelper/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2927:1
Spacebars.call@http://localhost:3000/packages/spacebars.js?7bafbe05ec09b6bbb6a3b276537e4995ab298a2f:172:12
@http://localhost:3000/template.forms.js?70bd177091ff1ea210ad3e4ae259b54f523adf01:42:12
Blaze.If/</<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2626:44
viewAutorun/</<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1865:16
Template._withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:12
viewAutorun/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1864:1
Blaze._withCurrentView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2197:12
viewAutorun@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1863:1
Tracker.Computation.prototype._compute@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:327:5
Tracker.Computation@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:243:5
Tracker.autorun@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:566:11
Blaze.View.prototype.autorun@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1875:14
Blaze.If/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2625:1
fireCallbacks@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1941:9
Tracker.nonreactive@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:593:12
Blaze._fireCallbacks/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1938:5
Blaze._withCurrentView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2197:12
Blaze._fireCallbacks@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1937:3
Blaze._createView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1955:3
Blaze._materializeView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1998:3
materializeDOMInner@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1476:9
Blaze._materializeDOM@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1428:7
Blaze._materializeView/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2040:25
Tracker.nonreactive@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:593:12
Blaze._materializeView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2004:3
Blaze.render@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2296:3
Template.body.renderToDocument@http://localhost:3000/packages/templating.js?376767bb0d2463b3b2615a1b90e77f6b22d39d7b:105:14
ready/runStartupCallbacks@http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:785:8
ready@http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:787:3
 meteor.js:888:11
Exception from Tracker afterFlush function: meteor.js:888:11
TypeError: d[0] is undefined meteor.js:888:11
b.init/<@http://localhost:3000/packages/materialize_materialize.js?76da33aa1a8c219b9d3d6cde626664cb18f469c0:38:3543
.each@http://localhost:3000/packages/jquery.js?dd8bac56f8fd3666d433d2285ae01e52597cc51a:417:14
jQuery.prototype.each@http://localhost:3000/packages/jquery.js?dd8bac56f8fd3666d433d2285ae01e52597cc51a:169:10
b.init@http://localhost:3000/packages/materialize_materialize.js?76da33aa1a8c219b9d3d6cde626664cb18f469c0:38:3098
a.fn.tabs@http://localhost:3000/packages/materialize_materialize.js?76da33aa1a8c219b9d3d6cde626664cb18f469c0:38:4767
@http://localhost:3000/forms.js?bb20f376b815e838347b13b36da9b44e65fad3d9:21:5
fireCallbacks/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3155:9
Template._withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:12
fireCallbacks@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3151:1
Template.prototype.constructView/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3244:5
Blaze.View.prototype.onViewReady/fire/</<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1778:11
Blaze._withCurrentView@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2197:12
Blaze.View.prototype.onViewReady/fire/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1777:9
Tracker._runFlush@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:501:11
onGlobalMessage@http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:372:11
 meteor.js:888:11
TypeError: d[0] is undefined

Upvotes: 0

Views: 99

Answers (1)

255kb - Mockoon
255kb - Mockoon

Reputation: 6974

While you can rely on Meteor.userId() to check if a user is logged in or not as it is always available, Meteor.user() is actually a mongo collection like any other. Thus, it may take some time for this collection to be ready and data to be retrieved from the server.

To solve this helpers are rerun reactively as soon as the reactive data used inside change. So I would get rid of the try/catch part and just return u.username == "admin" with a "guard" so it checks the username only when the data is available (i.e. u !== null), like this:

Template.tabs.helpers({
  isAdmin: function() {
    var u = Meteor.user();
    return u && u.username == "admin";
  }
});

Upvotes: 1

Related Questions