【问题标题】:Template.templatename.rendered can't return any data from mongodbTemplate.templatename.rendered 无法从 mongodb 返回任何数据
【发布时间】:2013-06-05 17:09:44
【问题描述】:

我想知道如何在 Template.templatename.rendered 函数中从 mongodb 获取数据。我在其他模板上尝试了单击事件,一切正常并返回我想要的结果。但我需要的是在加载时渲染图表。但我无法从 mongodb 获取任何数据。

//poll.js

    var drawPollChart = function(){

    //returns data on other template methods except for 
    //Template.templatename.rendered
    var dist = getDistinctQuestionId();
    alert('dist:'+dist);
    var data_x =[];
    for(var i=0; i< 1; i++)
    {
          var count = getDataCount(dist[i]);
            var uniq = getDistinctResponseBucket(dist[i]);
            for(var j=0; j<uniq.length; j++)
            {
                //alert('data:' + count[uniq[j]] + ", label:" + uniq[j]);
                data_x.push({
                    data : count[uniq[j]], 
                    label: uniq[j]
                });
            }
    }

Template.pollChart.rendered = function() {
    //can't draw a thing cause can't get any data from mongodb
    drawPollChart();
};

请帮忙?提前致谢。

【问题讨论】:

    标签: mongodb meteor meteorite


    【解决方案1】:

    仅仅因为模板被渲染并不意味着数据库已连接。

    使用 Meteor.status().status 来检测连接的状态。例如,您可以等到 Meteor.status().status === 'connected' 之后再渲染 pollChart 模板。

    【讨论】:

    猜你喜欢
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2020-01-26
    • 2012-04-02
    • 2021-06-24
    • 1970-01-01
    相关资源
    最近更新 更多