Insert works but find and fetch doesnot return anything

I’m using Meteor 1.6. I created a collection called ‘Post’ and when I go to the console window and try to insert data as in "Post.insert({content: ‘my post’}) and press enter it return the unique id suggesting insert is success. But when I type "Post.find().count()"it returns 0 instead of 1. Where am I going wrong? This is how it goes:

Woah, Meteor 1.6! Are you from the future? :stuck_out_tongue:

This usually happens when you don’t have your publications and subscriptions set up correctly.

sorry, when I checked the version it showed 1.6. … I was confused too thinking I’m sure downloaded 1.5 :stuck_out_tongue:

I’m new to Meteor, so please bear with me :slight_smile: I’m following a tutorial, and in that the insert worked fine for the instructor and he didn’t mention anything about publications and subscriptions. May I know how to set up.

It’s possible that the instructor had different set of packages installed. Probably he had autopublish and insecure and you don’t. And no, please don’t install them back, keep them out of your code, just set the publications and subscriptions up.

for testing only, you should add autopublish and insecure packages to see the result of your find query

What happens when you don’t have the correct publications and subscriptions set up (and aren’t using the autopublish package) Is that the documents first get inserted into both the server database and the client database. Then the server sends a database update to the client, but if your publications/subscriptions aren’t set up correctly, the client doesn’t have access to the documents, which means that the server will say to the client that its current subscription has zero results, and the client will then remove the documents from its local database.

I setup publish/subscribe and tried the execution again, Again same result, but interestingly meteor informs that since autopublish is still installed, I remove it if I want to go ahead with publish and subscribe. So my question is if autopublish was turned on then why did I get the above error at all.

I think this behaviour is more to do with the insecure package, which is used to allow any client to mutate the server’s database.

If insecure is not installed, client changes will be rolled back if the mutation is denied by the server (which by default will be what happens).

Then you should see an access denied message in the console though.

1 Like

I get no such error. Anyhow what to do now :nerd:

Is your code in a github repo we can look at?

Nah! I’m just a newbie trying my hand for the first time on Meteor. I didn’t get far enough to put it on github :slight_smile:

Okay - so which tutorial are you following?

“Learn Web Development by Creating a Social Network” by Pablo Farias at Udemy. His teaching is pretty well and so far I didnt encounter any errors, and I’m more than 70% through the course. Ofcourse now I got stuck though!

That’s because he uses meteor 1.2 and mine is 1.5, although he advised in the course that shouldn’t make much difference and if any he would mention.

It is just possible the tutorial is at fault, but that seems to be a paid-for course, so I’m not going to be able to look at it.

Did you get through 70% of the tutorial without using insert/find? Or did it work before, on other collections?

You could try checking out another tutorial and compare, like leveluptuts on youtube.

1 Like

Thank you. I will try that now.

Hey guys,

So I tried to import Post.js file at both client and server in main.js and it worked. The above problem seems to be resolved, i.e insert works, count return right value.

So I tried to get that displayed on my browser using following code:

But it does not display anything on the browser.

Please check the code whether it’s correct and let me know what went wrong.
Thank you