[SOLVED] Collection.find().fetch() returns []


#1

Hello. I’ve got a meteor project with react and flow router but there is a problem with the database: if I do a console.log(collection.find().fetch()) returns []. I removed autopublish so I publish my collection in the lib/collection.js file that I import in the server/main.js and I subscribe to it using FlowRouter in lib/routes.js that is imported in client/main.js:

if(Meteor.isServer){
console.log(“Publishing ‘Projects’ collection”);
Meteor.publish(‘Projects’, () => {
return Projects.find();
});
}
FlowRouter.route(’/’, {
name: ‘home’,
subscriptions() {
console.log(“Subscribing to ‘Projects’ collection”)
this.register(‘Projects’, Meteor.subscribe(‘Projects’));
},
action() {
mount(MainLayout, {content: , })
}
});

What is the problem?


#2

In general, it is not recommended to run subscriptions on the router level in FlowRouter. Your code looks ok, though. But you don’t provide the code of the actual template calling the collection.find(). I guess the problem lies there.


#3

Oh okay. The collection.find is in the action() in the router:
http://pastebin.com/jx8734Hj
The ProjectsComponent has a propTypes called projects of type PropTypes.array
if it’s not recommended to run the subscriptions in FlowRouter, where should I run it?


#4

I had the same problem ! Try this link
Hopefully this helps you :slight_smile:


#5

Thank you this helped a lot. Now I subscribe in FlowRouter in action() using the tracker and it works :slight_smile: but using a form in the same page, it doesn’t automatically update. Why is that?


#6

FlowRouter isn’t reactive so when something changes, FlowRouter doesn’t update the view.

You should read through the Meteor Guide, especially the react chapter.


#7

How should I do it then? Where should I subscribe to the collection so that it’s reactive?


#8

Read the meteor guide. Everything you need to know is in there.


#9

Thank you :slight_smile: I’ve read again the meteor guide and I finally understood. I now have a createContainer. That’s where I subscribe to my collection that I return to my component