【问题标题】:Meteor method clobbers subscribed queryMeteor 方法clobbers 订阅查询
【发布时间】:2016-02-11 11:25:37
【问题描述】:

我正在使用 Meteor 和 React。当我加载页面时,Iron Router 会在集合中创建一个文档。然后它使用 Meteor.method 来查找用户的权限,并通过和更新将它们设置在文档上。同时,客户端页面正在加载并运行对集合中文档的查询。

似乎 Meteor.method 代码正在破坏客户端上的查询。通过多次查询,文档存在,我可以 console.log() 它。然后突然我得到“未定义”。该文档仍然存在,因为我可以从另一个 Meteor.call 中查询它。我用过发布/订阅,但是客户端看不到。

这是我的一些代码(为简单起见合并文件):

Editors = new Mongo.Collection('editors');

if (Meteor.isServer())
    Meteor.publish('editors', function (editorId) {
        return Editors.find(editorId) || this.ready();
    });

    Meteor.methods({
        setEditorMode: function (editorId, userId) {
            var editor = Editors.findOne(editorId);
            var role = Roles.find({
                doc_id: editor.manuscriptId,
                user_id: userId
            }).fetch()[0];
            var defaultEditorMode = "readOnly";

            if (role !== null && role.role == 'owner' || role.role == 'contributor') {
                defaultEditorMode = "readWriteComment";
            } else if (role !== null && role.role == 'reviewer') {
                defaultEditorMode = "readComment";
            }

            console.log(editor);
            Editors.update(editor._id, {
                $set: {
                    defaultEditorMode: defaultEditorMode,
                    editorMode: defaultEditorMode
                }
            });
            console.log(Editors.findOne(editor._id);
        }
    });
}

if (Meteor.isClient()) {
    Router.route('/documents/:id/edit', function () {
        var editorId;

        manuscriptId = this.params.id;

        this.wait(/* Doing some work */);

        if (this.ready()) {
            editorId = Editors.insert({manuscriptId: manuscriptId});
            Meteor.call('setEditorMode', editorId, Meteor.userId());
            this.render('editor', {
                data: function () {
                    return {
                        editorId: editorId
                    };
                }
            });
        } else {
            this.render('loading');
        }
    });

    Editor = React.createClass({
        mixins: [ReactMeteorData],

        componentWillMount: function () {
            Meteor.subscribe('editors', this.props.editorId);
        },

        getMeteorData: function () {
            var editor, editorId;

            editorId = this.props.editorId;
            editor = Editors.findOne(editorId);
            console.log(editor);

            return {
                editor: editor || {}
            };
        },

        render: function () {
            return <EditorContent editor={this.data.editor} />
        }
    });
}

我需要在服务器上进行更新,以便安全地进行。我的期望是服务器上文档的任何更新都会传播到客户端。相反,一旦我在服务器上进行更新,getMeteorData() 就会被调用并且查询返回“未定义”。

如何获取我刚刚在服务器上设置的值?

【问题讨论】:

  • 更新:我已经在 Meteor.methods 中注释掉了更新,但我仍然遇到这个问题。在进一步的测试中,我已经安排从客户端对文档进行额外的更新,但是当我从客户端查询时,除了“未定义”之外,我从来没有得到任何东西(服务器仍然会得到更改)。如何调试 Meteor.subscribe?
  • 我想您尝试通过不同的组件来控制台记录您的 editorId,并确保在您的 mongoDB/meteor shell 中该条目是正确的插入器和更新。您是否尝试将订阅放入您的 getMeteorData 或删除 || this.ready() 来自出版物?我不熟悉你的风格,但这些是我唯一不确定的地方。

标签: javascript meteor reactjs


【解决方案1】:

感谢@ko0stick,我发现我需要将 Meteor.subscribe 代码放在 getMeteorData 中

【讨论】:

    猜你喜欢
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2013-01-18
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多