Collection not defined on Meteor Mongo

Hey guys,

Something weird is happening!
When I try to run operations (find, insert…) with my collections on meteor mongo I get an error message:

2018-12-20T16:14:30.894-0200 E QUERY    [js] ReferenceError: Rooms is not defined :
@(shell):1:1

(“Rooms” is the collection’s name)

All my collections are empty (nothing has been stored in it). I tried meteor reset, but it still doesn’t work.
I also tried to put the collection on the window object, with no success.

My collections.js file is in /lib and has the following structure:

import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'simpl-schema';
import '../imports/api/methods.js';

export const Rooms = new Mongo.Collection('rooms');

const RoomsSchema = new SimpleSchema({
    name: {
        type: String
    },
    type: {
        type: String
    }
});

Rooms.attachSchema(RoomsSchema);

Any idea what could be happening?

Collections are created lazily by Meteor. So they won’t exist until you insert something.

I tried to insert a document on the collection through meteor mongo, but it gives me the same error.
Then I created a form to insert it through the app itself (localhost), it worked (checked through logs), but it keeps giving me the same error on mongo…

I also get the same error on the console when trying to do anything with a collection:

> Rooms.find({})
VM1320:1 Uncaught ReferenceError: Rooms is not defined
    at <anonymous>:1:1

You are mixing the concept of the code running inside Nodejs and the mongo shell. In the mongo shell you won’t have access to variables declared inside your Meteor app code.

Try this instead…

> db.Rooms.find()
3 Likes