【问题标题】:Tracker.autorun not working inside componentDidMount of reactTracker.autorun 在 react 的 componentDidMount 中不起作用
【发布时间】:2018-11-28 17:48:03
【问题描述】:

当我指定输出的投影(字段)时,Tracker.autorun 在 react 的 componentDidMount 内不起作用。但是当我对 mongo 查询没有任何预测时,同样有效。

这行得通:

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

这不起作用

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();

我在这里错过了什么?

【问题讨论】:

    标签: reactjs meteor mongodb-query meteor-tracker


    【解决方案1】:

    我不认为 Meteor 的跟踪器与 ReactJS 配合得很好,因为它们的重新渲染机制不同。

    你可能想使用这个包。

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

    你可以这样使用它。

    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);
    

    IndexPage 是您的实际组件。

    然后您可以通过this.props.Meteor.collection.whatever.find() 访问数据库

    【讨论】:

      猜你喜欢
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 2021-01-29
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      相关资源
      最近更新 更多