Noob Question: Mongo collections not persisting?

Hello! So I am currently working through a Meteor tutorial and I seem to be having an issue with a Mongo collect I created with the following code:

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

The collection itself works fine in the sense where I can manipulate it in the console and add/remove/fetch/etc. documents . However, anytime there is a refresh of the page, or I open localhost:3000 on another tab, the inserted data is not there, i.e not being persisted.

I assume this is something super simple or just some code I need to add in but Google wasn’t able to help me out. So any help would be great :slight_smile:

How are you adding your documents to the collection? Can you post your code for doing that?

In the console I’m doing both

PlayerList.insert({name: "Name", score: 0});

and

PlayerList._collection.insert({name: "Name", score: 0});

Both seem to produce the same results but no information persists.

Are you implying that what you inserted into the collection on the console is not persisted? Do you not get any error messages on the browser console when you refresh the page?

Yup, that is what’s happening. And no errors are reported. And when I check the collection’s contents in the console in a new tab or after a page refresh it shows as empty.

Are you sure that you’re subscribed to receive the documents? Turn on auto publish to automatically get all documents from the server. It just sounds like you’re not pub/subbing correctly.

Never turned auto publish off, but also never did anything really involving publishing/subbing yet.

Did you try to use db.players.insert({…}) on the command line?

Doing that gives me an error of “db is undefined”.

In another terminal window, go to your project directory and type ‘meteor mongo’ (without the quotes) and press enter. Then enter the query and it should work.

1 Like

Still getting the same error :confused:

HOWEVER >> Doing

PlayerList.insert({name:"Name", score: 0})

is now working properly after typing in what you recommended in the terminal (meteor mongo).
Thank you! :slight_smile:

I am glad you got it sorted out.

1 Like

it’s always good to see a Padawan can call call for help from his Jedi Master… :grinning:

1 Like