2

当我为输出指定投影(字段)时,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();

我在这里想念什么?

4

1 回答 1

0

我不认为 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()

于 2018-06-25T01:20:17.010 回答