Search code examples
javascriptmongodbmeteormeteor-blaze

Meteor js - unable to insert data on Mongo through console


I'm following an udemy course on Meteor.

Steps to replicate my problem:

1) On CMD:

meteor create Leaderboard
cd Leaderboard
meteor npm install
meteor run
(server starts, localhost:3000)

2) Then, I deleted the contents of main.css, main.html & main.js (under client folder).

3) Opened main.js and added the following line:

PlayersList = new Mongo.Collection('players');

On Chrome's console I typed 'PlayersList' and got the following:

M…o.Collection {_transform: null, _connection: Connection, _collection: LocalCollection, _name: "players", _driver: LocalCollectionDriver…}

THE PROBLEM

When I type this on console:

PlayersList.insert({ name: 'David', score: 0 });

This is the Error:

"eebRFhA9vbSfHzPKk"
meteor.js?hash=e3f53db…:930 insert failed: Method '/players/insert' not found

Why am I getting it? I followed the exact steps provided by the instructor. What's wrong here?


Solution

  • That error is caused by the collection not having been defined on the server, only on the client.

    The default app created by meteor create app contains two main.js files, one in the client and one in server folder. These specially-named folders work exactly as you'd expect - the content in them is only loaded on the client or server. Apparently you added the collection definition only to the one in client. To fix this, if you put a file outside these specially-named folders - say create a folder named collections, and add a PlayersList.js file there and define your collection there, then it will be loaded by both and work.

    Alternatively (this is the recommended method when developing a larger app) if you put the file that defines the collection into a folder named imports then you can import that to your server and client code separately, instead of Meteor auto-including it. You can read more about Meteor application structure here.