Cyph
Cyph

Reputation: 631

meteor.js using aldeed:tabular with RemoteCollectionDriver

I'm encountering an issue when trying to populate data from by collection to the aldeed:tabular module for meteor.

My code looks like this and is the root of the project as a common.js file.
All the code below is in a single file: 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"}
    ]
});

The Tabular code must be in common code any visible to the server and client but when I run the above the "idpool" collection is not defined (out of scope.)

Reference Error: idpool is not defined

Moving the DB declaration outside the scope to the top of the JS, then I cant publish and subscribe to it. i.e. :

database = new MongoInternals.RemoteCollectionDriver("mongodb://localhost:27017/adaptiveid");
idpool = new Mongo.Collection("idpool", {_driver:database});
//rest of the code.....

And if I try to add the a second time by the common Tabular portion of the code like this:

idpool = new Mongo.Collection("idpool");

I get the following error :

Error: A method named '/idpool/insert' is already defined

What am I doing wrong here? How do I declare the DB server side and expose it to the common tabular code.

Upvotes: 2

Views: 518

Answers (3)

Philip
Philip

Reputation: 71

I too got stuck, the documentation simply says "Define your table in common code" -- all very true, but equally important is that both the server and client must have access to your collection.

So I found, define all my collections in a single file creating a 'lib/collections.js' to worked well for me, so I knew were they existed and as I refactored I didn't loose track of when they loades, so I would have something like:

export const idpoll = new Mongo.Collection('idpool');
export const otherCollection = new Mongo.Colletionc('myOtherCollectin');

Then anywhere else in my code that if i needed that collection i just imported it:

import { idpoll } from './lib/collections.js'  //ie. path to collections.js

In my project I have multiple tables, thus I decided to create a lib/init-tables.js file

export let TabularTables = {};
Meteor.isClient && Template.registerHelper('TabularTables',TabularTables);

Then for each table I have a lib/myNameTable.js file along with initial settings.

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'},

   ...

  ]
});

Upvotes: 0

Ethaan
Ethaan

Reputation: 11376

You should place the code inside the /lib folder

  /lib
  if(Meteor.isServer){
       database = new        MongoInternals.RemoteCollectionDriver("mongodb://localhost:27017/adaptiveid");
      idpool = new       Mongo.Collection("idpool",   {_driver:database});
      //rest of the code.....
   }

Why the isServer and the /lib? Well the /lib folder is the very first meteor loads at the start, but that code is shared between client/server thats why you should specify it to use that code only in the server

Note the Error: A method named '/idpool/insert' is already defined cames here because you are declaring the collection idpool twice.

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

You already declare the collection there, why are you declaring it twice?, just remove the second idpoll = new Mongo.Collection('idpool')

Upvotes: 2

Karan R
Karan R

Reputation: 37

I'm sure it's standard practice to declare new collections in the lib folder so they get created before rest of the code runs Move the idpool line to lib and retry

Upvotes: -1

Related Questions