0

I'm trying to save a collection in my Meteor application.

So far I have this simple bit of code:

MyCollection = new Mongo.Collection("Dogs"); console.log(MyCollection.find({}));

This creates the collection as the console log confirms its existence. However, it isn't creating it in my specified Mongo URL.

I am running the application locally and specifying the Mongo URL with the following command:

MONGO_URL='<MyURL>' meteor

This string seems to work as when I signed up a user to the application a users collection was created as well as a meteor_accounts_loginServiceConfiguration collection within my specified database.

Collections

However, the code to create the collection 'Dogs' - Which is attached to a button click - Fails to create a new collection in the database but it's being created elsewhere.

I can only assume this is being created in the local Mongo provided by Meteor?

Also, I have tried setting up my settings.json file with the following:

{
  "galaxy.meteor.com": {
    "env": {
      "MONGO_URL": "<MongoURL>"
    }
  }
}

And pointed the application to it during deployment. This also seems to have worked as creating a new user updates the 'Users' collection in my database. However, the issue still persists that the 'Dogs' collection is not being created in my specified Mongo database.

Does any body know why this may be the case? What am I missing?

Many thanks for your help,

G

UPDATE

I tried this method:

var database = new MongoInternals.RemoteCollectionDriver("<mongo url>");
MyCollection = new Mongo.Collection("collection_name", { _driver: database });

Which I found here: Using Multiple Mongodb Databases with Meteor.js

But this fails to create any collection in my MongoDB either.

Another Update

After looking at this question:

https://forums.meteor.com/t/how-to-view-collection-contents-using-console-log/23852/12

I tried the following:

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.

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.

Any suggestions?

Thank you!:)

GeorgeBT
  • 121
  • 2
  • 20

2 Answers2

1

Did you say you're writing to the database from the client? If that's the case, then make sure you are allowing client-side writes, or better create a method on the server that writes to the database, and your client calls that method.

What I think is happening is that it is being written on the minimongo instance on the client side, and this is not being synced with the database on your server.

d4nyll
  • 11,811
  • 6
  • 54
  • 68
  • I am writing from the client yes. Please could you explain how I would write/call a method? – GeorgeBT Jul 18 '17 at 13:26
  • Hi @d4nyll could you look at my update to the question - Is this the sort of thing you meant? – GeorgeBT Jul 18 '17 at 13:35
  • It's moving in the write direction. You use a Meteor method to write / update the database, and you use publication / subscription to allow clients to read from the database. You might have gotten them mixed up a little there. I have wrote a more in-depth tutorial for an older version of Meteor - https://scotch.io/tutorials/building-a-slack-clone-in-meteor-js-getting-started - it might be worth taking a look, as it explains these concepts in more details than the tutorial on meteor.com – d4nyll Jul 18 '17 at 14:30
  • Thank you - I will take a look at your tutorial now – GeorgeBT Jul 18 '17 at 14:44
0

The issue has now been resolved - Thanks to some very useful help from Rob Fallows.

The file structure has been changed about a little and now contains the following folders and files:

  • imports/task.js
  • client/main.js
  • server/main.js

The line export const Tasks = new Mongo.Collection('tasks'); sits in the imports/task.js file as it needs to be executed on both the client and the server side. The publish and subscribe then comes into play as the (minimongo) collection on the client will then be sync'd with the actual database collection.

So now the files look like so:

task.js...

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

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

client/main.js...

import { Tasks } from '/imports/tasks';

Meteor.subscribe('tasks');

window.Tasks = Tasks;

server/main.js...

import { Tasks } from '/imports/tasks';

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

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

When the application is served from the command line using the MONGO_URL='<MyURL>' meteor command, a tasks node is made visible in the MongoDB database.

And now when the console command Tasks.find().fetch(); is entered on the client side in the browser the data is returned:

Console Log

The full answer in more detail can be found here

GeorgeBT
  • 121
  • 2
  • 20