Meteor-插入失败:找不到方法

Meteor-插入失败:找不到方法,meteor,Meteor,我的Meteor的JS文件有问题。当我尝试将任何数据插入数据库并在图表上反映时,会出现此错误“insert failed:Method not found”。我尝试过直接从数据库中获取数据,但效果不太好。。。 提前准备好 LinePeople=newmongo.Collection(“LinePeople”); 函数getRandomInt(最小值、最大值){ 返回Math.floor(Math.random()*(max-min+1))+min; } if(Meteor.isClient){

我的Meteor的JS文件有问题。当我尝试将任何数据插入数据库并在图表上反映时,会出现此错误“insert failed:Method not found”。我尝试过直接从数据库中获取数据,但效果不太好。。。 提前准备好

LinePeople=newmongo.Collection(“LinePeople”);
函数getRandomInt(最小值、最大值){
返回Math.floor(Math.random()*(max-min+1))+min;
}
if(Meteor.isClient){
console.log(“在线客户端”);
//LinePeople=newmongo.Collection(空);
Template.linenvd3.rendered=函数(){
var chart=nv.models.lineChart()
.margin({left:80})//调整图表边距以给x轴一些喘息的空间。
.useInteractiveGuideline(true)//我们需要好看的工具提示和指南!
.transitionDuration(350)//您希望线路以多快的速度过渡?
.showLegend(true)//显示图例,允许用户打开/关闭线系列。
.showYAxis(true)//显示y轴
.showXAxis(true)//显示x轴
;
nv.addGraph(函数(){
chart.xAxis.axisLabel('Person number').tickFormat(d3.format('d'));
chart.yAxis.axisLabel('Age(years')).tickFormat(d3.format('d'));
d3.选择(“#线形图svg”).基准面(
[{values:LinePeople.find().fetch(),键:'Age'}]
).电话(图表);
windowResize(函数(){chart.update()});
收益表;
});
副自动运行(函数(){
d3.选择(“#线形图svg”).基准面(
[{values:LinePeople.find().fetch(),键:'Age'}]
).电话(图表);
chart.update();
});
};
Template.linenvd3.events({
“单击#添加数据按钮”:函数(){
console.log(“在线添加按钮”);
变量年龄=getRandomInt(13,89);
var lastPerson=LinePeople.findOne({},{fields:{x:1},sort:{x:-1},limit:1,reactive:false});
如果(最后一个人){
console.log(“在lastPerson..if block中”);
LinePeople.insert({x:(lastPerson.x+1),y:age});
}否则{
console.log(“在lastPerson..else块中”);
LinePeople.insert({x:1,y:age});
}
},
“单击#removeDataButton”:函数(){
console.log(“在线删除按钮”);
var lastPerson=LinePeople.findOne({},{fields:{x:1},sort:{x:-1},limit:1,reactive:false});
如果(最后一个人){
LinePeople.remove(lastPerson.\u id);
}
}
});
}
if(Meteor.isServer){
console.log(“在线服务器”);
}

谢谢你的帮助。。。事实上,我通过发布集合并授予它一些权限来实现它:

此代码位于“myapp/shared/collections.js”中。(将它们分开放置以处理我将为其他图形添加的所有其他集合)

此代码位于“myapp/server/publish.js”中

然后,对Javascript进行了修改,使其看起来更简单、更全面

if (Meteor.isClient) {
Meteor.subscribe('line');
Template.linenvd3.rendered = function() {

    var chart = nv.models.lineChart()
      .margin({left: 80})
      .useInteractiveGuideline(true)
      .transitionDuration(350)
      .showLegend(true)
      .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: lineVar.find().fetch(), key: 'Age' }]
          ).call(chart);
          nv.utils.windowResize(function() { chart.update() });
          return chart;
    });

    Deps.autorun(function () {
          d3.select('#lineChart svg').datum(
            [{ values: lineVar.find().fetch(), key: 'Age' }]).call(chart);
          chart.update();
    });
};
}
在官方meteor.js网站上的入门教程之后,我在打开autopublish时遇到了同样的问题

问题是我在
imports/
文件夹中创建了我的任务集合。因此,它不是隐式导入到服务器上的

为了解决这个问题,我必须在服务器上显式导入它

server/main.js

import { Meteor } from 'meteor/meteor';
import '../imports/api/tasks.js';

Meteor.startup(() => {  
  // code to run on server at startup
});

正如您所见,我的代码不使用导入,但无论如何都是必需的。

您的项目中是否有活动的
autopublish
?(
meteor list
)也可以指向代码中发生错误的那一行?你确定它来自meteor而不是d3或nv吗?是的,autopubish与nvd3js和d3一起处于活动状态。它没有指向代码中的任何一行,但我可以通过“inspect元素”在控制台中看到该错误。我不确定这是否是meteor/d3/nv的问题,因为我是meteor的新手。我甚至尝试过由LinePeople手动插入数据。insert({“x”:1,“y”:39”})…当然,结果是一样的。你能把你的问题简化为要点吗?如果你只是无法执行
LinePeople.insert({“x”:1,“y”:39”})
而不会出现错误,并且数据库中不会发生插入,然后,请删除所有其他不必要的代码,以便于人们尝试和复制您的错误。实际上,您应该在自己的帖子中添加最后一条注释作为答案。这一切与d3无关。问题是你,这应该被标记为正确答案。你解决了我宝贵的编码时间。在数小时的痛苦和绝望之后。。。您的答案拯救了一条生命,使其免于完全黑暗:)在学习入门教程时发现了此问题。起初一切正常,但在做了一些更改后,它停止了工作。按照这个答案进行操作修复了这个问题,但我不明白为什么一开始它没有明确地授予对服务器上集合的访问权就可以工作。
if (Meteor.isClient) {
Meteor.subscribe('line');
Template.linenvd3.rendered = function() {

    var chart = nv.models.lineChart()
      .margin({left: 80})
      .useInteractiveGuideline(true)
      .transitionDuration(350)
      .showLegend(true)
      .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: lineVar.find().fetch(), key: 'Age' }]
          ).call(chart);
          nv.utils.windowResize(function() { chart.update() });
          return chart;
    });

    Deps.autorun(function () {
          d3.select('#lineChart svg').datum(
            [{ values: lineVar.find().fetch(), key: 'Age' }]).call(chart);
          chart.update();
    });
};
}
import { Meteor } from 'meteor/meteor';
import '../imports/api/tasks.js';

Meteor.startup(() => {  
  // code to run on server at startup
});