GeorgeBT
GeorgeBT

Reputation: 121

Meteor - Collections not saving to specified Mongo URL

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!:)

Upvotes: 0

Views: 429

Answers (2)

GeorgeBT
GeorgeBT

Reputation: 121

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

Upvotes: 0

d4nyll
d4nyll

Reputation: 12617

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.

Upvotes: 1

Related Questions