Tracker.autorun not working inside componentDidMount of react

243 Views Asked by At

Tracker.autorun not working inside componentDidMount of react when I specify the projection (fields) for output. But the same works when I dont have any projection on mongo query.

This works:

Meteor.subscribe('quotes');
        this.quotesTracker = Tracker.autorun(() => {
            const quotes = Quotes.find(
                {instrument_token: 12374274},
                {
                    sort: {timestamp: 1},
                    limit: 5000
                }
            ).fetch();

This doesnt work

Meteor.subscribe('quotes');
    this.quotesTracker =Tracker.autorun(() => {
            const quotes = Quotes.find(
                {instrument_token: 12374274},
                {
                    fields: {
                        last_price: 1,
                        timestamp: 1,
                                           },
                    sort: {timestamp: 1},
                    limit: 5000
                }
            ).fetch();

What am I missing here?

1

There are 1 best solutions below

0
On

I don't think Meteor's tracker works well with ReactJS, as their mechanism of re-render is different.

You might want to use this package.

https://github.com/meteor/react-packages/tree/devel/packages/react-meteor-data

You can use it like so.

import { Meteor } from 'meteor/meteor';
import { mount } from 'react-mounter';
import { withTracker } from 'meteor/react-meteor-data';
import { IndexPage } from "./index-page";

FlowRouter.route('/', {
    action: () => {
        mount(IndexPageContainer, {});
    }
});

export const IndexPageContainer = withTracker(() => {
    Meteor.subscribe('whatever');
    return {
        Meteor: {
            collection: {
                whatever: Whatever.find().fetch()
            },
            user: Meteor.user(),
            userId: Meteor.userId(),
            status: Meteor.status(),
            loggingIn: Meteor.loggingIn()
        }
    };
})(IndexPage);

Where IndexPage is your actual component.

You can then access the db by this.props.Meteor.collection.whatever.find()