Search code examples
meteorminimongo

Meteor minimongo insert method not working


I was following the meteor tutorial from meteortips and I got to the part where you create a collection in the browser's console. Creating the collection works, but it doesn't let me insert anything into it.(PlayersList = new Meteor.Collection('players');)

Please see below:

PlayersList.insert({ name: 'Alex', score: 42 });
"rpPamgZEZM9opCzHz"
debug.js:41 insert failed: Method not found

What's weirder is that I even get back the hash as if the insert worked. Typing PlayersList.find().fetch(); returns an empty array :(

I'm using the latest version of Meteor on Windows 8.1 with MongoDB version 2.6

If anybody could help me, I would be very thankful :)


Solution

  • You have defined the collection PlayersList = new Meteor.Collection('players'); on the client but it has not been defined on the server.

    If you have something like if(Meteor.isClient) {..} (or in the /client) directory the code won't run on the server. Make sure you also place a PlayersList = new Meteor.Collection('players'); in the if(Meteor.isServer) (or the /server) directory.

    The best thing to do is place it outside both in the root directory so it runs on both the client and server.

    When you insert the document on the client the message is transmitted to the the server & it tries to insert it into the database. The collection isn't defined on the server side so it rejects it with the message method not found.