No such template error when calling handlebars helper from Mongodb collection with Meteor
我有一个HTML模板,我想在其中显示使用车把帮助器存储在mongo对象中的值。
HTML-我想将答案1显示为存储在mongo中"问题1"下的字符串。
1 2 3 4 5 6 7 8 9 10
| <template name="preferencesView">
Here are your preferences
<p> Answer to question 1 {{> answer1}}</p>
<p> Answer to question 2 {{> answer2}}</p>
</template> |
这是我的.js帮助器。 (为什么它不将answer1作为模板存储在这里,而{{answer1}}可以在我的HTML文档中调用?)
1 2 3 4 5 6
| Template.preferencesView.helpers = ({
answer1: function () {
return Preferences_col.find({userId: Meteor.userId()}
);
}
}); |
也-我是否在上述帮助程序中使用正确的mongo调用来查找所需的数据? (问题1)这是在表单提交事件中创建的示例。
1
| {"Question1" :"Yes","Question2" :"Yes","userId" :"hqWJNWLjwfggJtbPi","submitted" : 1389406102126,"_id" :"fWEpMXX4w9wfzLg5g" } |
当我在终端中运行流星时,它没有遇到任何错误。但是,在我的浏览器控制台中,我收到"来自Deps重新计算的异常:错误:无此类模板'answer1'"
PS-这是我关于Stackoverflow的第一篇文章,因此,如果您需要更多信息,请告诉我!
如果我是对的,那么您就说错了。 {{> templateName}}与调用模板一起使用,而{{helperName}}与调用助手一起使用。同样,除了返回find查询的结果之外,还返回findOne查询的结果(作为find查询的结果),即使大小仅为1,也会有一些问题。 >
- 谢谢,它修复了没有此类模板的错误。但是,当我致电{{answer1}}时,仍然无法显示任何内容。我是否需要指定我要它找到Question1?返回Preferences_col.findOne({Question1:"
-
我可以通过以下方式将其作为对象返回到浏览器控制台中:------ Preferences_col.findOne({userId:Meteor.userId()},{fields:{Question1:1}}但是我如何显示它字符串"是"
-
尝试var prefs = Preferences_col.findOne({userId: Meteor.userId()}); return (prefs || {}).Question1;。您需要返回感兴趣的字段的值,而不是整个对象。
-
@alanning无效。我发现了另一个问题,他们在哪里使用这种格式来获取要显示的字段的值。但这对我也不起作用。 Preferences_col.find({userId: Meteor.userId()},{Question1:1, userId:0});
-
尝试一些调试。在新控制台中,cd到您的流星目录,然后输入meteor mongo。在那附近摆弄。确保实际上存在一个问题,该问题具有与当前Meteor.userId()匹配的userId。再一次,获得问题1的代码为Preferences_col.findOne({userId: Meteor.userId()}).Question1
-
@mjkaufer是的,在终端和浏览器控制台中,我可以提交db.preferences_col.findOne({userId:"x9G3EpTt8brjLyupt
-
@Vitiell0 if you can post your code up in a public repo we can help debug what's going wrong.
-
@alanning Sorry I should have done that in the OP. Here it is: github.com/Vitiell0/drawbridge2
-
This part is incorrect: Template.preferencesView.helpers = ({...})。它应该是:Template.preferencesView.helpers({...})。我调试它的方式是使用'answer1'函数return 'test';并注意到即使这样也无法正常工作,因此请查看原因。
-
@alanning谢谢你!谢谢你,这么简单的错误。即使修复了该问题,它也没起作用,直到昨天我用prefs变量插入了原始注释中的代码。我非常感谢您的帮助。