【问题标题】:Meteor - insert failed: Method not found流星 - 插入失败:找不到方法
【发布时间】: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


【解决方案1】:

在关注流星.js 官方网站上的入门教程时,我在打开自动发布时遇到了同样的问题。

原来问题是我在 imports/ 文件夹中创建了我的 Tasks 集合。因此它没有被隐式导入到服务器上。

我必须在服务器上显式导入它才能解决问题。

server/main.js

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

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

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

【讨论】:

  • 这应该被标记为正确答案。你解决了我宝贵的编码时间。
  • 经过数小时的痛苦和绝望……您的回答从完全的黑暗中拯救了一条生命:)
  • 在做入门教程时发现了这个。起初一切正常,但在进行了一些更改后,它停止了工作。按照这个答案的建议解决了这个问题,但我不明白为什么它确实在没有明确授予对服务器上集合的访问权限的情况下开始工作。
【解决方案2】:

感谢您的帮助...实际上,我通过发布集合并授予它一些权限来实现它:

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

lineVar = new Meteor.Collection("linenvd3");
lineVar.allow({
         insert: function () {
         return true;
         },
         update: function () {
         return true;
         },
         remove: function () {
         return true;
         }
         });

这段代码放在“myapp/server/publish.js”中

Meteor.publish('line', function () {
           return lineVar.find();
           });

然后,这是经过修改的 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();
    });
};
}

【讨论】:

猜你喜欢
  • 2013-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
  • 2016-08-07
  • 2014-06-30
  • 1970-01-01
相关资源
最近更新 更多