为什么这个模板助手会产生错误但仍然有效?
Why does this template helper produce an error but still work?
我从 Meteor 中的一个模板助手那里得到了一个异常 - 尽管我看到我的日志很好。
Template.DashboardHeader.helpers({
avatar: function () {
if (Meteor.user().profile.image){
console.log('yea');
} else {
console.log('nah');
}
}
})
我最终会用它来提供头像,这取决于用户是否在他们的帐户中设置了头像。助手实际上按照您的预期工作,并根据情况记录正确的输出,但是每当它是 运行:
时我都会得到这个无用的大异常
Exception in template helper: .avatar@http://localhost:3000/client/master.js?86599d1d506dfe7d57211f3faa8757db9ba5cb81:16:9
BlazeProvider.helpers/http://localhost:3000/packages/meteorhacks_kadira-debug.js?ee5ca93234e8f5b94a8e5560eb5ea8b7fcbb9c4d:339:26
... a lot more lines later...
Tracker._runFlush@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:485:9
onGlobalMessage@http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:372:11
鉴于它并没有真正告诉我任何事情(我可以 see/understand),我不确定要排除什么问题!
这是模板代码(已简化):
<template name="DashboardHeader">
<img id="user-img" src="{{ avatar }}" alt="{{ currentUser.profile.name }}" width="34px" height="34px" />
</template>
尽管 {{ avatar }}
位于模板标签内的任何位置都会触发异常。但如果模板中不存在,不会触发。所以它似乎与助手本身的关系不如它的应用方式。
有人知道这是怎么回事吗?
这种错误(控制台中出现巨大异常,但模板中出现数据)通常是由于 undefined
并被查询的反应性依赖引起的。
举个例子:
Template.myTemplate.helpers({
'name' : () => MyCollection.findOne().name
})
根据数据可用性或订阅策略,上述内容可能会引发异常,因为 MyCollection.findOne()
在首次调用时为 undefined
。
然而,一旦数据可用(订阅开始),正确的输出仍然会出现。 findOne
使其依赖项(包括助手)无效,并且由于 name
现在存在,它将显示在模板中。
在您的情况下,这意味着 Meteor.user()
或 Meteor.user().profile
在首次调用时未定义。
要轻松解决你可以使用一些 short-circuiting :
//Will return undefined if findOne() is undefined
() => (MyCollection.findOne() && MyCollection.findOne().name)
当然还有其他方法可以实现这种 undefined
检查。
您似乎也在使用 Firefox。
Meteor 在某些浏览器上喷出 hideous stack traces 而没有错误原因。
在 Chrome 上,这就是我的示例中的此类错误:
Exception in template helper: TypeError: Cannot read property 'name' of undefined
at (... stacktrace ...)
因此,当您看到巨大的难以理解的字符串连接堆栈跟踪时,请检查您是否可以在其他浏览器上获得更多有用的信息。
我已经在 MeteorPad 上构建了此行为的完整演示。
我从 Meteor 中的一个模板助手那里得到了一个异常 - 尽管我看到我的日志很好。
Template.DashboardHeader.helpers({
avatar: function () {
if (Meteor.user().profile.image){
console.log('yea');
} else {
console.log('nah');
}
}
})
我最终会用它来提供头像,这取决于用户是否在他们的帐户中设置了头像。助手实际上按照您的预期工作,并根据情况记录正确的输出,但是每当它是 运行:
时我都会得到这个无用的大异常Exception in template helper: .avatar@http://localhost:3000/client/master.js?86599d1d506dfe7d57211f3faa8757db9ba5cb81:16:9
BlazeProvider.helpers/http://localhost:3000/packages/meteorhacks_kadira-debug.js?ee5ca93234e8f5b94a8e5560eb5ea8b7fcbb9c4d:339:26
... a lot more lines later...
Tracker._runFlush@http://localhost:3000/packages/tracker.js?6d0890939291d9780f7e2607ee3af3e7f98a3d9c:485:9 onGlobalMessage@http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:372:11
鉴于它并没有真正告诉我任何事情(我可以 see/understand),我不确定要排除什么问题!
这是模板代码(已简化):
<template name="DashboardHeader">
<img id="user-img" src="{{ avatar }}" alt="{{ currentUser.profile.name }}" width="34px" height="34px" />
</template>
尽管 {{ avatar }}
位于模板标签内的任何位置都会触发异常。但如果模板中不存在,不会触发。所以它似乎与助手本身的关系不如它的应用方式。
有人知道这是怎么回事吗?
这种错误(控制台中出现巨大异常,但模板中出现数据)通常是由于 undefined
并被查询的反应性依赖引起的。
举个例子:
Template.myTemplate.helpers({
'name' : () => MyCollection.findOne().name
})
根据数据可用性或订阅策略,上述内容可能会引发异常,因为 MyCollection.findOne()
在首次调用时为 undefined
。
然而,一旦数据可用(订阅开始),正确的输出仍然会出现。 findOne
使其依赖项(包括助手)无效,并且由于 name
现在存在,它将显示在模板中。
在您的情况下,这意味着 Meteor.user()
或 Meteor.user().profile
在首次调用时未定义。
要轻松解决你可以使用一些 short-circuiting :
//Will return undefined if findOne() is undefined
() => (MyCollection.findOne() && MyCollection.findOne().name)
当然还有其他方法可以实现这种 undefined
检查。
您似乎也在使用 Firefox。
Meteor 在某些浏览器上喷出 hideous stack traces 而没有错误原因。
在 Chrome 上,这就是我的示例中的此类错误:
Exception in template helper: TypeError: Cannot read property 'name' of undefined
at (... stacktrace ...)
因此,当您看到巨大的难以理解的字符串连接堆栈跟踪时,请检查您是否可以在其他浏览器上获得更多有用的信息。
我已经在 MeteorPad 上构建了此行为的完整演示。