Meteor/VueJS 发布特定项目
Meteor/VueJS publish specific item
我正在尝试发布与当前事件关联的用户
但是我觉得我还没有完全理解meteor和vuejs之间的发布订阅原理
服务器端:
import { Meteor } from 'meteor/meteor';
import Events from './collections';
import moment from "moment";
const currentWeek = moment().week();
const currentYear = moment().year();
const currentEvent = Events.findOne({ week: currentWeek, year: currentYear })
Meteor.publish('currentUser', function () {
return Meteor.users.findOne({ _id: currentEvent.user_id } );
});
客户端:
export default {
meteor: {
$subscribe: {
currentUser: [],
},
currentUser() {
return Events.findOne()
}
}
}
客户端 returns 集合的第一个用户,而不是作为发布参数给出的事件的预期用户。
你能告诉我我错在哪里吗?我很难记录我的良好做法
这里有两个问题:
首先,发布旨在 return 游标(使用 .find
)而不是单个文档(使用 .findOne
完成)。
其次,服务器上的当前代码仅使用启动/导入模块时的时刻,因为代码在全局范围内并且不会重新执行。因此,如果您的服务器运行时间超过一周/一年,它就不会 return 准确的文档集。
为了 return 所有匹配查询的文档使用 .find
。为了始终获得当前时刻,您可以将其包装在一个函数中。总结代码可能如下所示:
import { Meteor } from 'meteor/meteor';
import Events from './collections';
import moment from "moment";
const getCurrentEvent = () => {
const currentWeek = moment().week();
const currentYear = moment().year();
return Events.findOne({ week: currentWeek, year: currentYear });
}
Meteor.publish('currentUser', function () {
const currentEvent = getCurrentEvent()
return Meteor.users.find({ _id: currentEvent.user_id });
});
进一步阅读:
我正在尝试发布与当前事件关联的用户 但是我觉得我还没有完全理解meteor和vuejs之间的发布订阅原理
服务器端:
import { Meteor } from 'meteor/meteor';
import Events from './collections';
import moment from "moment";
const currentWeek = moment().week();
const currentYear = moment().year();
const currentEvent = Events.findOne({ week: currentWeek, year: currentYear })
Meteor.publish('currentUser', function () {
return Meteor.users.findOne({ _id: currentEvent.user_id } );
});
客户端:
export default {
meteor: {
$subscribe: {
currentUser: [],
},
currentUser() {
return Events.findOne()
}
}
}
客户端 returns 集合的第一个用户,而不是作为发布参数给出的事件的预期用户。
你能告诉我我错在哪里吗?我很难记录我的良好做法
这里有两个问题:
首先,发布旨在 return 游标(使用 .find
)而不是单个文档(使用 .findOne
完成)。
其次,服务器上的当前代码仅使用启动/导入模块时的时刻,因为代码在全局范围内并且不会重新执行。因此,如果您的服务器运行时间超过一周/一年,它就不会 return 准确的文档集。
为了 return 所有匹配查询的文档使用 .find
。为了始终获得当前时刻,您可以将其包装在一个函数中。总结代码可能如下所示:
import { Meteor } from 'meteor/meteor';
import Events from './collections';
import moment from "moment";
const getCurrentEvent = () => {
const currentWeek = moment().week();
const currentYear = moment().year();
return Events.findOne({ week: currentWeek, year: currentYear });
}
Meteor.publish('currentUser', function () {
const currentEvent = getCurrentEvent()
return Meteor.users.find({ _id: currentEvent.user_id });
});
进一步阅读: