在回调函数中呈现 Meteor 检索到的数据的正确语法是什么

what is the proper syntax to present data retrieved by meteor inside a callback function?

本文关键字:数据 是什么 语法 检索 函数 回调 Meteor      更新时间:2023-09-26

在 y meteor 应用程序中,我需要从 MongoDB 检索数据,然后将这些数据提供给 Template.chart.render 中的各种图表函数。目前,我的(结构不正确的代码如下所示:

Template.chart.rendered = function () {
    var yelp_data = Yelp.find().fetch();
    ...
    var ndx = crossfilter(yelp_data);
}

此结构不允许我的代码等到检索数据,然后在回调上下文中提供该数据。检索数据并将所有后续处理包装在回调中的正确语法是什么?

等待订阅以确保所有数据都存在。 模板文档

Template.chart.onRendered(function () {
 var template = this;
 template.subscribe('yelp', function () {  // Wait for the data to load using the callback
     yelp_data = Yelp.find().fetch();
     var ndx = crossfilter(yelp_data)
 });
});