Javascript meteor.js使用aldeed:tabular和RemoteCollectionDriver

Javascript meteor.js使用aldeed:tabular和RemoteCollectionDriver,javascript,meteor,Javascript,Meteor,我在尝试将数据从by collection填充到meteor的模块时遇到了一个问题 我的代码如下所示,是作为common.js文件的项目根。 以下所有代码都在一个文件中: root/lib/hello.js if (Meteor.isServer) { Meteor.startup(function () { // code to run on server at startup database = new MongoInternals.RemoteCol

我在尝试将数据从by collection填充到meteor的模块时遇到了一个问题

我的代码如下所示,是作为common.js文件的项目根。 以下所有代码都在一个文件中: root/lib/hello.js

if (Meteor.isServer) {
    Meteor.startup(function () {
    // code to run on server at startup

          database = new MongoInternals.RemoteCollectionDriver("mongodb://localhost:27017/mydb");
          idpool = new Mongo.Collection("idpool", {_driver:database});

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

    });
}

if (Meteor.isClient) {

    Meteor.subscribe("idpool");
}


TabularTables = {};

Meteor.isClient && Template.registerHelper('TabularTables', TabularTables);

TabularTables.idpool = new Tabular.Table({
    name: "idpool",
    collection: idpool,
    columns: [
        {data: "_id", title: "id"}
    ]
});
表格代码必须是对服务器和客户端可见的公共代码,但当我运行上述代码时,idpool集合的定义并不超出范围

Reference Error: idpool is not defined
将范围外的DB声明移动到JS的顶部,则我无法发布和订阅它。i、 e:

database = new MongoInternals.RemoteCollectionDriver("mongodb://localhost:27017/adaptiveid");
idpool = new Mongo.Collection("idpool", {_driver:database});
//rest of the code.....
如果我第二次尝试添加代码的公共表格部分,如下所示:

idpool = new Mongo.Collection("idpool");
我得到以下错误:

错误:已定义名为“/idpool/insert”的方法


我做错了什么?如何声明DB服务器端并将其公开给公共表格代码

您应该将代码放在/lib文件夹中

  /lib
  if(Meteor.isServer){
       database = new        MongoInternals.RemoteCollectionDriver("mongodb://localhost:27017/adaptiveid");
      idpool = new       Mongo.Collection("idpool",   {_driver:database});
      //rest of the code.....
   }
为什么是IServer和/lib?好的,/lib文件夹是最开始的meteor加载,但是该代码在客户机/服务器之间共享,这就是为什么您应该指定它仅在服务器中使用该代码的原因

注意错误:一个名为 此处已定义了“/idpool/insert”,因为您要两次声明集合idpool

idpool = new Mongo.Collection("idpool", {_driver:database})

您已经在那里声明了集合,为什么要声明两次呢?只需删除第二个idpoll=new Mongo.collection'idpool'

我确信在lib文件夹中声明新集合是标准做法,以便在其余代码运行之前创建它们
将idpool行移到lib并重试

我也被卡住了,文档只是说用通用代码定义表-所有这些都是正确的,但同样重要的是,服务器和客户端都必须能够访问您的集合

因此,我发现,在一个文件中定义我的所有集合,创建一个“lib/collections.js”来为我工作,这样我就知道它们存在,并且在重构时,我没有忘记它们加载的时间,所以我会有这样的结果:

export const idpoll = new Mongo.Collection('idpool');
export const otherCollection = new Mongo.Colletionc('myOtherCollectin');
然后在我的代码中的任何其他地方,如果我需要该集合,我只需导入它:

import { idpoll } from './lib/collections.js'  //ie. path to collections.js
在我的项目中,我有多个表,因此我决定创建一个lib/init-tables.js文件

export let TabularTables = {};
Meteor.isClient && Template.registerHelper('TabularTables',TabularTables);
然后,对于每个表,我都有一个lib/myNameTable.js文件以及初始设置

import { TabularTables } from './init-tables.js';  // Import TabularTables helper
import { idpoll } from './collections.js';

TabularTables.Regs = new Tabular.Table({
  name: 'IDPOL',
  collection: idpol,
  columns: [
    {data: 'field', title: 'fieldtitle'},line'},

   ...

  ]
});

我只在遇到第一个错误后尝试声明它两次…我得到了一个错误,因为我之前得到了一个不同的错误,如上所述。ReferenceError:未定义idpool,因为它在ifMeteor.isServer声明中超出范围。只需将ifMeteor.isServer{dbcodehere}放在/lib文件夹中,并在代码已在lib dir中时声明一次即可。最初的问题是tabletables.idpool=new Tabular.Table{name:idpool,collection:idpool,模板上是否有waiton?当数据库尚未在客户端上发布/订阅时,您正在尝试访问数据库