Reputation: 61
I am taking the first steps in learning how to develop within the meteor.js framework using my Windows 10 PC.
In windows, when creating a new app, the system creates a folder with separate sub-folders for client and server .js files.
My question is if I define a new Mongo collection inside the server.js file, how can I access that collection from the client.js file?
Upvotes: 0
Views: 51
Reputation: 2386
what you're asking is OS-agnostic.
i think you already know that files within a folder called "server" are not seen by the client, and likewise files within a folder called "client" are not seen by the server.
Meteor will eagerly serve files outside such folders to both the client and the server (unless it's in a folder called "imports", more on that in a moment).
so if your project is set up with top-level folders called "client" and "server", it is common to make a folder called "collections", also at the top-level, to define the collections.
so let's say you have a file called collections/News.js:
News = new Mongo.Collection('news');
when that file is served to the server, it will create that collection in Mongo. when that file is served to the client, it will create a local collection in minimongo and associate it with the real collection. in both instances, "News" is a global variable you can access from anywhere.
so that should answer your question.
going further, MDG is recommending a new directory structure going forward. you can read about it here: https://guide.meteor.com/structure.html
in short, they want us to move to a model where files are not eagerly loaded, but explicitly imported by our code. during the transition period, we are meant to put our files into /imports. files in there are not eagerly loaded.
using the same example above, "News" would probably exist in its own area, as a module, in a file like this:
imports/api/news/News.js
const News = new Mongo.Collection('news');
export {News};
here, the file is not eagerly imported, but whatever code relies on News would have to import that module:
import {News} from '/imports/api/news/News';
that import would work in both client and server code.
Upvotes: 1