My app has a list of polls and I am passing the data as following:
export default class App extends TrackerReact(Component) {
constructor() {
super();
this.state = {
subscription: {
pollsData: Meteor.subscribe('polls', {sort: {createdAt: -1},limit: POLLS_PER_PAGE * pageNumber.get()}) //add this when removing autopublish and insecure
}
}
}
componentWillUnmount() {
this.state.subscription.pollsData.stop();
}
pollsData() {
return PollsData.find({}, {sort: {createdAt: -1}, limit: POLLS_PER_PAGE * pageNumber.get()}).fetch();
}
increaseLimit() {
if(POLLS_PER_PAGE * pageNumber.get() < PollsData.find().count()) pageNumber.set(pageNumber.get() + 1);
console.log(" LIMIT " + POLLS_PER_PAGE * pageNumber.get());
}
showMoreButton() {
if(POLLS_PER_PAGE * pageNumber.get() < PollsData.find().count())
return (
<div className="load-more">
<button className="btn btn-more" onClick={this.increaseLimit}>Load More</button>
</div>
)
}
render() {
return (
<div className="">
<Polls polls={this.pollsData()} />
{this.showMoreButton()}
</div>
);
}
}
That works fine. Then, to show only one poll I have another component that looks very similar:
import React, { Component, PropTypes } from 'react';
import TrackerReact from 'meteor/ultimatejs:tracker-react';
import PollsData from '/imports/api/polls.js';
import ViewPoll from "./ViewPoll.jsx";
export default class ViewPollContainer extends TrackerReact(Component) {
constructor() {
super();
const id = FlowRouter.getParam('id');
this.state = {
subscription: {
pollData: Meteor.subscribe('singlePoll', id) //add this when removing autopublish and insecure
}
}
}
componentWillUnmount() {
this.state.subscription.pollData.stop();
}
pollData() {
const id = FlowRouter.getParam('id');
return PollsData.findOne(id).fetch();
}
render() {
return (
<ViewPoll poll={this.pollData()} />
);
}
}
This however is giving me an error. It says:
ViewPollContainer.jsx:46 Uncaught TypeError: Cannot read property ‘fetch’ of undefined
This refers to
return PollsData.findOne(id).fetch();
In Constellation, I can see the data for the poll I am trying to display and under Subscriptions (also in constellation) I can see singlePoll with the id as params.
Then I replaced PollsData.findOne(id).fetch(); with a poll object as
poll = {
_id: this.props.id,
question: "Color",
options: [
{
option: "white",
id: 1
},
{
option: "black",
id: 2
}
]
}
return poll;
And it works. So it seems the problem is with the publish method. PollsData is:
import PollsData from '/imports/api/polls.js';
Meteor.publish('polls', () => {
return PollsData.find();
});
Meteor.publish('singlePoll', (id) => {
check(id, String);
return PollsData.find({_id: id});
});
But even if I replace the subscription on the ViewPollContainer component to subscribe to polls. it doesn’t work, while it works on the App component.
I cannot find what is wrong, and I would appreciate suggestions to fix it.
I also tried to manage the data using data containers, but it didn’t work either.