Meteor:模板帮助程序中的异常

Meteor:模板帮助程序中的异常,meteor,Meteor,这是流星的密码 Template.message.helpers({ message: function() { var message = Messages.findOne({ _id: FlowRouter.getParam('messageId')}); var curTime = new Date(); console.log(message.createdAt.getHours()); return messag

这是流星的密码

Template.message.helpers({
    message: function() {
        var message = Messages.findOne({ _id: FlowRouter.getParam('messageId')});
        var curTime = new Date();
        console.log(message.createdAt.getHours());

        return message;
    }
});
给我一个例外:

Exception in template helper: message@http://localhost:3000/app/app.js?hash=0da6be4507e857f169e0cecb7b0874729eae4663:239:13
bindDataContext/<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3051:14
Blaze._wrapCatchingExceptions/<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1715:14
wrapHelper/</<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3103:14
Template._withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3744:12
wrapHelper/<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3102:12
Spacebars.dot@http://localhost:3000/packages/spacebars.js?hash=ebf9381e7fc625d41acb0df14995b7614360858a:234:13
template.message.js/Template.message</<@http://localhost:3000/app/app.js?hash=0da6be4507e857f169e0cecb7b0874729eae4663:85:31
doRender@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2086:20
viewAutorun/</<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1934:18
Template._withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3744:12
viewAutorun/<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1932:14
Blaze._withCurrentView@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2271:12
viewAutorun@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1931:12
Tracker.Computation.prototype._compute@http://localhost:3000/packages/tracker.js?hash=997515fa2d5b0530ba07741da556c4b36963ef3b:339:5
Tracker.Computation@http://localhost:3000/packages/tracker.js?hash=997515fa2d5b0530ba07741da556c4b36963ef3b:229:5
Tracker.autorun@http://localhost:3000/packages/tracker.js?hash=997515fa2d5b0530ba07741da556c4b36963ef3b:613:11
Blaze.View.prototype.autorun@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1944:14
Blaze._materializeView/<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2080:5
Tracker.nonreactive@http://localhost:3000/packages/tracker.js?hash=997515fa2d5b0530ba07741da556c4b36963ef3b:640:12
Blaze._materializeView@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2079:3
materializeDOMInner@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1532:9
Blaze._materializeDOM@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1474:3
Blaze._materializeDOM@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1483:7
Blaze._materializeView/<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2113:25
Tracker.nonreactive@http://localhost:3000/packages/tracker.js?hash=997515fa2d5b0530ba07741da556c4b36963ef3b:640:12
Blaze._materializeView@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2079:3
Blaze.render@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2370:3
_render@http://localhost:3000/packages/kadira_blaze-layout.js?hash=dbd1396d04e62378fc8792cdef18869a1108cedd:204:5
render/</<@http://localhost:3000/packages/kadira_blaze-layout.js?hash=dbd1396d04e62378fc8792cdef18869a1108cedd:77:9
withValue@http://localhost:3000/packages/meteor.js?hash=27829e936d09beae3149ecfbf3332c42ccb1596f:1077:17
withoutInvocation/<@http://localhost:3000/packages/meteor.js?hash=27829e936d09beae3149ecfbf3332c42ccb1596f:464:26
Meteor.bindEnvironment/<@http://localhost:3000/packages/meteor.js?hash=27829e936d09beae3149ecfbf3332c42ccb1596f:1105:17
onGlobalMessage@http://localhost:3000/packages/meteor.js?hash=27829e936d09beae3149ecfbf3332c42ccb1596f:401:11
  meteor.js:930:11
但时间仍然会打印到控制台上

我的目标是使用createdAt日期从当前日期对象中减去它,并获得时间增量。但我从来没有走那么远,因为无论我如何处理message.createdAt,都会产生这个奇怪的异常

如果我只是执行console.log(message.createdAt),我会在浏览器控制台中看到两个ISO格式的日期和两个异常

如果我尝试message.createdAt.getTime()之类的其他方法,我会得到一个Unix时间戳

到底发生了什么事


实际上,我可以减去日期并按预期使用它,但尽管代码正常工作,但异常仍不断出现。

您看到错误的原因是正在运行帮助程序,试图从尚未准备好的订阅中获取数据。它最终会起作用,因为一旦订阅就绪,助手就会重新运行,一切都会好起来。要解决此问题,只需使用
模板包装帮助程序。subscriptionsReady

<template name="message">
  {{#if Template.subscriptionsReady}}
    {{message}}
  {{else}}
    {{> SomeLoadingTemplate}}
  {{/if}
</template>

{{{#if Template.subscriptionsReady}
{{message}}
{{else}
{{>SomeLoadingTemplate}
{{/if}
注意:此模板仅在所有 模板级订阅已“就绪”,并将显示 执行else部分的加载程序模板



关于对这个问题的评论,用
if(date){…}
:这将消除错误,但并不能真正解决问题。帮助程序仍在不必运行的情况下运行多次。隐藏这样的潜在错误似乎有点困难。

您有一个模板、一个帮助程序和一个局部变量,都称为“message”。即使这不是您错误的根源,我也会温和地建议将它们命名为不同的名称。

您会看到重复的名称,因为帮助程序正在运行多次(由于反应性更改).什么是JS日期对象
createdAt
?它来自mongo集合,在mongo shell中显示为ISODate,但我假设它是JS中的日期对象。我可以使用一些日期属性,如year和getTime()在上面。在呈现/调用该助手之前,您是否正在等待订阅完成?谢谢@chazsolo,因为您将我引向了正确的方向。我在谷歌上搜索等待meteor订阅,发现了Template.subscriptionsReady,它使我将模板包装在一个条件中,只有subscriptionsReady为true时才输出数据这就消除了异常。我现在知道每次渲染都会出现异常,直到数据准备就绪。我是meteor的新手,这是我的第一个项目。请回答,这样我可以标记它。
var date=message&message.createdAt;if(date){console.log(new date(date).getHours();}
应该停止给你那个错误。否决我的人能解释一下为什么这是个错误的建议吗?
<template name="message">
  {{#if Template.subscriptionsReady}}
    {{message}}
  {{else}}
    {{> SomeLoadingTemplate}}
  {{/if}
</template>