Noob Question: Mongo collections not persisting?


#1

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:


#2

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


#3

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.


#4

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?


#5

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.


#6

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.


#7

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


#8

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


#9

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


#10

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.


#11

Still getting the same error :confused:


#12

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:


#13

I am glad you got it sorted out.


#14

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