Why does Meteor say my Collection is not defined?

#1

I have a Meteor app with a server folder; in that server folder, I added a .js file with this content:

PostTravel = new Mongo.Collection('postTravel');

if (PostTravel.find().count() === 0) {
  PostTravel.insert({
    title: 'Introducing Telescope',
    url: 'http://sachagreif.com/introducing-telescope/'
  });

  PostTravel.insert({
    title: 'Meteor',
    url: 'http://meteor.com'
  });

  PostTravel.insert({
    title: 'The Meteor Book',
    url: 'http://themeteorbook.com'
  });
}

Note: I adapted this code from here: http://es.discovermeteor.com/chapters/collections/

When Meteor tries to run this new code, though, it fails, telling me, “ReferenceError: PostTravel is not defined

Here is a dump of the pertinent communique from the command prompt:

C:\Users\clayshan\Meteor\posttravel>meteor

=> Started proxy.
=> Started MongoDB.
=> Exited with code: 8
W20150812-11:27:35.357(-7)? (STDERR)
W20150812-11:27:35.362(-7)? (STDERR) C:\Users\clayshan\AppData\Local\.meteor\pac
kages\meteor-tool\1.1.4\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\
fibers\future.js:245
W20150812-11:27:35.363(-7)? (STDERR)
throw(ex);
W20150812-11:27:35.363(-7)? (STDERR)
      ^
W20150812-11:27:45.686(-7)? (STDERR) ReferenceError: PostTravel is not defined
W20150812-11:27:45.687(-7)? (STDERR)     at app\server\loaddata.js:1:40
W20150812-11:27:45.687(-7)? (STDERR)     at app\server\loaddata.js:18:3
W20150812-11:27:45.687(-7)? (STDERR)     at C:\Users\clayshan\Meteor\posttravel\
.meteor\local\build\programs\server\boot.js:222:10
W20150812-11:27:45.687(-7)? (STDERR)     at Array.forEach (native)
W20150812-11:27:45.688(-7)? (STDERR)     at Function._.each._.forEach (C:\Users\
clayshan\AppData\Local\.meteor\packages\meteor-tool\1.1.4\mt-os.windows.x86_32\d
ev_bundle\server-lib\node_modules\underscore\underscore.js:79:11)
W20150812-11:27:45.688(-7)? (STDERR)     at C:\Users\clayshan\Meteor\posttravel\
.meteor\local\build\programs\server\boot.js:117:5
=> Exited with code: 8

So what am I doing wrong here?

#2

You’re likely referencing PostTravel in the loaddata.js file before it’s had a chance to load. You might want to consider moving your Collection definition into the lib directory. See http://docs.meteor.com/#/full/fileloadorder for more details.

#3

Thanks, that did it; “timing is everything.” - Clay ShannonMark Twain Central

#4

this doesn’t work for me. I have the same problem, my collections are “not defined” despite me putting them in a collection.js file in the lib folder (which has worked several times before!) suddenly it doesn’t, have tried re-installing meteor, anyone any idea?

thanks in advance

#5

Please share your folder structure.

#6

it’s actually a standard app created by “meteor create”. only added a “lib” folder with only the “collections.js” in there.
so it’s
.meteor
client
lib
node_modules
server
test

haven’t edited any file besides collections etiher, and if i try “Cards.find().fetch()” or even only “Cards” which is my collection in the console, it reveals the error message.
i also tried to rename “lib” to “collections” because i saw that in some tutorials, but it (unsurprisingly) didnt change anything

#7

What version of Meteor are you using?

And, are you using import/export syntax?

#8

i’ve only just reinstalled it as a try to fix the issue, so i have 1.7.0.1 now. so far, i only have the import statements which the standard app comes with and in my collection.js i have

import { Mongo } from ‘meteor/mongo’;

Cards = new Mongo.Collection(‘cards’);

#9

A new project created with Meteor 1.7 will use the new default lazy-loading, in which all files need specific import statements.

To restore “classic” behaviour, you will need to edit the package.json from this:

{
  "name": "mytest",
  "private": true,
  "scripts": {
    "start": "meteor run",
    "test": "meteor test --once --driver-package meteortesting:mocha",
    "test-app": "TEST_WATCH=1 meteor test --full-app --driver-package meteortesting:mocha",
    "visualize": "meteor --production --extra-packages bundle-visualizer"
  },
  "dependencies": {
    "@babel/runtime": "^7.0.0-beta.48",
    "meteor-node-stubs": "^0.4.1"
  },
  "meteor": {
    "mainModule": {
      "client": "client/main.js",
      "server": "server/main.js"
    },
    "testModule": "test/main.js"
  }
}

to this:

{
  "name": "mytest",
  "private": true,
  "scripts": {
    "start": "meteor run",
    "test": "meteor test --once --driver-package meteortesting:mocha",
    "test-app": "TEST_WATCH=1 meteor test --full-app --driver-package meteortesting:mocha",
    "visualize": "meteor --production --extra-packages bundle-visualizer"
  },
  "dependencies": {
    "@babel/runtime": "^7.0.0-beta.48",
    "meteor-node-stubs": "^0.4.1"
  }
}

In other words, remove the meteor stanza.

1 Like
#10

Thank you!
I tried this, and now it says

W20180607-11:35:32.435(2)? (STDERR) C:\Users\administrator\AppData\Local.meteor\packages\meteor-tool\1.7.0_1\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\fibers\future.js:280
W20180607-11:35:32.437(2)? (STDERR) throw(ex);
W20180607-11:35:32.438(2)? (STDERR) ^
W20180607-11:35:32.438(2)? (STDERR)
W20180607-11:35:32.439(2)? (STDERR) Error: Cannot find module ‘./main.html’
W20180607-11:35:32.439(2)? (STDERR) at makeMissingError (packages\modules-runtime.js:231:12)
W20180607-11:35:32.440(2)? (STDERR) at require (packages\modules-runtime.js:241:19)
W20180607-11:35:32.440(2)? (STDERR) at collections.js (collections/collections.js:1:193)
W20180607-11:35:32.441(2)? (STDERR) at fileEvaluate (packages\modules-runtime.js:339:7)
W20180607-11:35:32.441(2)? (STDERR) at require (packages\modules-runtime.js:238:16)
W20180607-11:35:32.442(2)? (STDERR) at C:\Users\administrator\Desktop\Neuer Ordner\mem.meteor\local\build\programs\server\app\app.js:132:1
W20180607-11:35:32.442(2)? (STDERR) at C:\Users\administrator\Desktop\Neuer Ordner\mem.meteor\local\build\programs\server\boot.js:411:36
W20180607-11:35:32.442(2)? (STDERR) at Array.forEach ()
W20180607-11:35:32.443(2)? (STDERR) at C:\Users\administrator\Desktop\Neuer Ordner\mem.meteor\local\build\programs\server\boot.js:220:19
W20180607-11:35:32.443(2)? (STDERR) at C:\Users\administrator\Desktop\Neuer Ordner\mem.meteor\local\build\programs\server\boot.js:471:5
W20180607-11:35:32.444(2)? (STDERR) at Function.run (C:\Users\administrator\Desktop\Neuer Ordner\mem.meteor\local\build\programs\server\profile.js:510:12)
W20180607-11:35:32.444(2)? (STDERR) at C:\Users\administrator\Desktop\Neuer Ordner\mem.meteor\local\build\programs\server\boot.js:470:11
=> Exited with code: 1
=> Your application is crashing. Waiting for file change.

#11

Did you forget to do meteor npm i first?

Also, I forgot to mention that restoring classic behaviour means you will get other errors if you leave the test/ folder there, so delete it, or move it into imports/, or rename it to .test/.

#12

thank you, everything is fixed now! can’t believe i forgot to edit package.json, has been a while since i’ve started a meteor project from scratch…

thanks so much!

1 Like
#13

You’re welcome.

Incidentally, I’ve raised an issue about the test folder behaviour.

#14

thank you, this solved quite a bit of things for me

1 Like