这个问题跟我前一个问题有关:Meteor: How to publish custom JSON data
我创建了一个构建复杂JSON(使用不同集合等构建)的Meteor方法 . 有了这些数据,我需要使用D3.js构建一个图表 .
我曾经在Session中设置数据,但是当我意识到每次到达页面时都需要刷新它,它无法正常工作 .
显然我在机制中有一些我不理解的东西,但这是我的代码:
Template.myPage.rendered = function(){
var data = Meteor.call('myData');
var svgContainer = d3.select("#svg");
var margin = 40;
var width = parseInt(d3.select("#svg").style('width'),10);
var height = parseInt(d3.select("#svg").style('height'),10);
// ... some more D3 code where I need the data
我得到的错误是:
"Exception from Tracker afterFlush function: data is undefined
I have already tried to call the Meteor Method outside the Template and put the result in the Session (in the callback function), but it did not work. I've also tried to use reactive var, but I couldn't make it.
非常感谢 .
4 回答
好吧,当您尝试使用没有值的变量时,会出现
"undefined"
错误 .根据你的说法,Meteor.method返回一个依赖于其他集合的
JSON
.所以我认为你在这里的最佳选择是在
myPage
模板上使用waitOn
.您可以尝试使用会话,也可以使用跟踪器 .
GL
我会尝试这样的smth:
Thanks to twitter and @callmephilip 我找到了答案(我很惭愧,我自己找不到!:D)
唯一要做的就是 put all the d3 code inside the method callback :
祝你有美好的一天 ;)
你可以使用"meteor-reactive-method" . 这是https://github.com/stubailo/meteor-reactive-method的内联链接
然后在客户端
在服务器中