Issues with Publish and Subscribe

Hi there,

I am trying to figure out how I can connect my Meteor application to an external MongoDB.

I’m very new to Meteor and am more of a Firebase person so please bear with me.

So far I have tried the following:

Upon running my application locally I run the following command…

MONGO_URL="<My Mongo URL Here>" meteor

… And within my application I had the following client side code…

Demo = new Mongo.Collection('demo');

This failed to create the collection in my specified MongoDB URL. Rather, I believe, it just created it in Meteor’s mini mongo?

So I then tried deploying the application to Meteor and pointed the application to my settings.json file which looks like so…

{
  "galaxy.meteor.com": {
    "env": {
      "MONGO_URL": "<My Mongo URL Here>"
    }
  }
}

… And again upon running the application it fails to create the collection in my specified MongoDB.

So I turned my attention to a question found here…

https://forums.meteor.com/t/how-to-view-collection-contents-using-console-log/?source_topic_id=37984

… And I have attempted to do a similar thing with subscribe and publish.

I created a folder called server and within it a file called main.js which contained the following:

const Tasks = new Mongo.Collection('tasks');

Tasks.insert({_id: 'my-todo'});

if (Meteor.isServer) {
  // This code only runs on the server
  Meteor.publish('tasks', function() {
    return Tasks.find();
  });
}

Within my application on the client side I added the following code to my JS file:

if (Meteor.isClient) {
  Meteor.subscribe('tasks');
}

According to the help forum this should work - I think… I’m not sure:\

It works partially - The tasks collection is created in my specified Mongo URL and the task is inserted into it.

However, I don’t know how to display this now on the client side.

Does any body know how I now display this data into my application?

Many thanks,

G

You need that const Tasks = new Mongo.Collection('tasks'); to be executed on the client and the server. You could:

  • Put that line into a file in client/ and server/ folders, or
  • Put it in a single file which will be built for client and server (so, something like a both/ folder), or
  • import into client and server code from a single file under the imports/ folder. This is the recommended method.

The (minimongo) collection on the client will then be sync’d with the actual database collection via the pub/sub you have defined and you should be able to access it on the client. If you want to work with it through the browser console you may need a window.Tasks = Tasks in your client code to put it into the global scope.

1 Like

Hi Rob,

Thank you for your reply. Could you please advise if this is what you meant when you said import into client and server code from a single file under the imports/ folder…

I’ve created an imports folder containing a file named imports.js which looks like this…

if (Meteor.isServer) {

    const Tasks = new Mongo.Collection('tasks');

    Tasks.insert({_id: 'my-todo'});

    // This code only runs on the server
    Meteor.publish('tasks', function() {
        return Tasks.find();
    });
}

if (Meteor.isClient) {

    const Tasks = new Mongo.Collection('tasks');

    Meteor.subscribe('tasks');

    window.Tasks = Tasks;
}

Is this anything like what you meant or am I totally off?

Thank you,

George

Well,I think that would probably work - but it’s not quite what I was thinking of!

// imports/Tasks.js
import { Mongo } from 'meteor/mongo';

export const Tasks = new Mongo.Collection('tasks');
// client/main.js
import { Tasks } from '/imports/Tasks';
// more code here
// server/main.js
import { Tasks } from '/imports/Tasks';
// more code here

So, more along those lines. As a rule I try to avoid Meteor.isClient and Meteor.isServer. It’s generally much cleaner (and more secure) to separate client and server code through the client/ and server/ folders.

1 Like

Thank you,

The only issue I seem to be having now is the terminal returns:

Error: Cannot find module '/imports/Tasks'

Forgive me - Was simply a mis match of file names.

Thank you very much for your assistance Rob! As always your answers have solved the problem.

Very much appreciated.

George

2 Likes