gpt4 book ai didi

javascript - Meteor 方法破坏订阅查询

转载 作者:行者123 更新时间:2023-11-28 06:24:28 25 4
gpt4 key购买 nike

我正在使用 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() 就会被调用并且查询返回“未定义”。

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

最佳答案

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

关于javascript - Meteor 方法破坏订阅查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35246466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com