如果用户是管理员,我使用条件格式设置了meteor中带有实体标签的模板。不幸的是,这只能间歇性地工作。有谁理解为什么?
模板:
<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;
}
});
然后我得到一些奇怪的输出(浏览器控制台)+ 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
答案 0 :(得分:1)
虽然您可以依靠Meteor.userId()
来检查用户是否已登录,因为它始终可用,Meteor.user()
实际上是一个mongo集合,与其他任何集合一样。因此,可能需要一些时间来准备此集合并从服务器检索数据。
要解决此问题,只要内部使用的反应数据发生变化,就会重新运行此帮助程序。所以我会摆脱try / catch部分,只需返回u.username == "admin"
和#34;后卫&#34;所以它只在数据可用时检查用户名(即u !== null
),如下所示:
Template.tabs.helpers({
isAdmin: function() {
var u = Meteor.user();
return u && u.username == "admin";
}
});