Meteor doesn't create collections

OK, so I’ve installed Differential Boilerplate and Robomongo (shouldn’t have to be relevant, but I mention it nevertheless). I’ve tried to create new collections inside Boilerplate and I couldn’t. Created a new basic Meteor project, copied code from first couple of steps of Meteor To-do tutorial, it doesn’t create a collection.

I can’t create a collection whatever I do, I can only access the ones I created before Differential and Robomongo. No errors in terminal.

What on earth could be the matter? I would open an issue on Differential GitHub, but I can’t believe this could be the cause.

I’m using this code:

Tasks = new Mongo.Collection("tasks");
1 Like

Have you tried inserting something? I don’t believe the collection is created in the mongo database until you insert something.

Task.insert({ foo: 'blah', bar: 'bleh' });

Sure, I get this:

db.tasks.insert({ text: "Hello world!", createdAt: new Date() });    

Uncaught ReferenceError: db is not defined(…)
        (anonymous function) @ VM2933:2
        InjectedScript._evaluateOn @ VM2932:875
        InjectedScript._evaluateAndWrap @ VM2932:808
        InjectedScript.evaluate @ VM2932:664

Your code needs to be under a server/ folder, or in a shared location (like lib/). If it’s under a client/ folder, or in a Meteor.isClient block it won’t create a collection in MongoDB.

In this case, you will need db and db.tasks defined to be visible to the server.

2 Likes

In Boilerplate it was under both/, in this simple app I don’t have any folders and not in .isClient. I’m aware databases have to be both server & client. I had no problems creating databases in Meteor before, problem started today after Boilerplate and Robomongo.

Only other thing I can think of: do you have the mongo package installed (can you see the mongo line in your .meteor.packages file)?

This is the crucial piece of information. There is no db in scope. Is it declared globally (wherever it’s defined)?

db = ...

not

var db = ...

@hellogerard
Well, yeah. But I had this also in my previous projects.

Yup.

Tasks = new Mongo.Collection("tasks");
1 Like

Your posts are ambiguous.

Are you using Tasks = new Mongo.Collection("tasks"); or db.tasks = new Mongo.Collection("tasks");?

Aha, you’ve hit the sweet spot!

As per Meteor tutorial you should (could) create a collection like this:

Tasks = new Mongo.Collection("tasks");

and insert (in console for instance) like this:

db.tasks.insert({ text: "Hello world!", createdAt: new Date() });

I get the same log if I do like this:

messages.insert({text: "Hello, world!"});

But as per documentation, it should be like this:

Tasks.insert({text: "Hello, world!"});

Which works and I have a db also in Robomongo.

Lesson: don’t rely on anything else but documentation.

Thanks guys for helping me out.

you should run the code from the Server side javascript file, if you are using latest version.

Interestingly, this was my issue. For weeks I’ve been scouring forums to fix the error of “Cannot Perform Insert On Undefined” from the CFS Autoform. The answer was to move the code which initializes the CollectionFS into the shared “lib” folder.

Thumbs = new FS.Collection("thumbs",{
    stores: [new FS.Store.FileSystem("thumbs",{path:"~/uploads"})]
});

Thanks much.

1 Like