【发布时间】:2018-06-14 16:31:32
【问题描述】:
我的 Meteor 的 JS 文件有问题。当我尝试将任何数据插入数据库并反映在图表上时,我收到此错误“插入失败:找不到方法”。我试过直接从数据库中获取数据,但也没有用…… 提前谢谢。
LinePeople = new Mongo.Collection("LinePeople");
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
if (Meteor.isClient) {
console.log("in LIne Client");
//LinePeople = new Mongo.Collection(null);
Template.linenvd3.rendered = function() {
var chart = nv.models.lineChart()
.margin({left: 80}) //Adjust chart margins to give the x-axis some breathing room.
.useInteractiveGuideline(true) //We want nice looking tooltips and a guideline!
.transitionDuration(350) //how fast do you want the lines to transition?
.showLegend(true) //Show the legend, allowing users to turn on/off line series.
.showYAxis(true) //Show the y-axis
.showXAxis(true) //Show the x-axis
;
nv.addGraph(function() {
chart.xAxis.axisLabel('Person number').tickFormat(d3.format('d'));
chart.yAxis.axisLabel('Age (years)').tickFormat(d3.format('d'));
d3.select('#lineChart svg').datum(
[{ values: LinePeople.find().fetch(), key: 'Age' }]
).call(chart);
nv.utils.windowResize(function() { chart.update() });
return chart;
});
Deps.autorun(function () {
d3.select('#lineChart svg').datum(
[{ values: LinePeople.find().fetch(), key: 'Age' }]
).call(chart);
chart.update();
});
};
Template.linenvd3.events({
'click #addDataButton': function() {
console.log(" in line addButton");
var age = getRandomInt(13, 89);
var lastPerson = LinePeople.findOne({}, {fields:{x:1},sort:{x:-1},limit:1,reactive:false});
if (lastPerson) {
console.log(" in lastPerson.. if block");
LinePeople.insert({x:(lastPerson.x + 1), y:age});
} else {
console.log(" in lastPerson.. else block");
LinePeople.insert({x:1, y:age});
}
},
'click #removeDataButton': function() {
console.log(" in line removeButton");
var lastPerson = LinePeople.findOne({}, {fields:{x:1},sort:{x:-1},limit:1,reactive:false});
if (lastPerson) {
LinePeople.remove(lastPerson._id);
}
}
});
}
if (Meteor.isServer) {
console.log("in line Server");
}
【问题讨论】:
-
您的项目中是否有
autopublish处于活动状态? (meteor list) -
另外,能否指出代码中出现错误的违规行?你确定它甚至来自流星而不是 d3 或 nv?
-
是的,自动发布与 nvd3js 和 d3 一起处于活动状态。它不指向代码中的任何行,但我可以通过“检查元素”在控制台中看到该错误。我不确定流星 / d3 / nv 是否有问题,因为我是流星新手。我什至尝试通过 LinePeople.insert({"x":1,"y":39"}) 手动插入数据...当然,结果是一样的。
-
那么您能否将您的问题简化为要点?如果您根本无法执行
LinePeople.insert({"x":1,"y":39"})而不会出现错误并且数据库中没有发生插入,那么请删除所有其他不必要的代码,以便人们尝试复制您的错误。 -
您实际上应该将最后一条评论添加为您自己帖子的答案
标签: meteor