2015-09-04 121 views
0

如果用戶是管理員,我使用條件格式在流星中設置了具有物化選項卡的模板。不幸的是,這隻能間歇性地工作。有誰明白爲什麼?用戶未定義流星

模板:

<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> 

的JS(管理員用戶登錄):

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; 
    } 
}); 

然後我得到一些奇怪的輸出(瀏覽器控制檯)+堆棧跟蹤:

user was undefined momentarily? forms.js:29:11 
undefined forms.js:30:11 
TypeError: u is undefined 
Stack trace: 
[email protected]://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 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:12 
wrapHelper/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2927:1 
[email protected]://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 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:12 
viewAutorun/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1864:1 
Blaze._withC[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2197:12 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1863:1 
[email protected]://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:327:5 
[email protected]://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:243:5 
[email protected]://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:566:11 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1875:14 
Blaze.If/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2625:1 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1941:9 
[email protected]://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:593:12 
Blaze._fireCallbacks/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1938:5 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2197:12 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1937:3 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1955:3 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1998:3 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1476:9 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1428:7 
Blaze._materializeView/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2040:25 
[email protected]://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:593:12 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2004:3 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2296:3 
[email protected]://localhost:3000/packages/templating.js?376767bb0d2463b3b2615a1b90e77f6b22d39d7b:105:14 
ready/[email protected]://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:785:8 
[email protected]://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:787:3 
forms.js:34:9 

Exception in template helper: [email protected]://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 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:12 
wrapHelper/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2927:1 
[email protected]://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 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:12 
viewAutorun/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1864:1 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2197:12 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1863:1 
[email protected]://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:327:5 
[email protected]://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:243:5 
[email protected]://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:566:11 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1875:14 
Blaze.If/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2625:1 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1941:9 
[email protected]://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:593:12 
Blaze._fireCallbacks/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1938:5 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2197:12 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1937:3 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1955:3 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1998:3 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1476:9 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1428:7 
Blaze._materializeView/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2040:25 
[email protected]://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:593:12 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2004:3 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2296:3 
[email protected]://localhost:3000/packages/templating.js?376767bb0d2463b3b2615a1b90e77f6b22d39d7b:105:14 
ready/[email protected]://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:785:8 
[email protected]://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 
[email protected]://localhost:3000/packages/jquery.js?dd8bac56f8fd3666d433d2285ae01e52597cc51a:417:14 
[email protected]://localhost:3000/packages/jquery.js?dd8bac56f8fd3666d433d2285ae01e52597cc51a:169:10 
[email protected]://localhost:3000/packages/materialize_materialize.js?76da33aa1a8c219b9d3d6cde626664cb18f469c0:38:3098 
[email protected]://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 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:12 
[email protected]://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 
[email protected]://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2197:12 
Blaze.View.prototype.onViewReady/fire/<@http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1777:9 
[email protected]://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:501:11 
[email protected]://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:372:11 
meteor.js:888:11 
TypeError: d[0] is undefined 

回答

1

儘管您可以依靠Meteor.userId()來檢查用戶是否已登錄或者因爲它始終可用, Meteor.user()實際上是一個像任何其他人一樣的mongo集合。因此,可能需要一些時間才能完成此集合,並且可以從服務器檢索數據。

爲了解決這個問題,只要內部使用的反應數據發生變化,就會反應性地重新運行。因此,我想擺脫的try/catch部分,就回到u.username == "admin"與「保護」,因此會檢查用戶名,只有當數據是可用的(即u !== null),像這樣:

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