我有一个简单的应用程序设置,显示一个列表Projects.我已经删除了autopublish包,所以我不会将所有内容发送给客户端.
<template name="projectsIndex">
{{#each projects}}
{{name}}
{{/each}}
</template>
Run Code Online (Sandbox Code Playgroud)
当autopublish打开时,这将显示所有项目:
if Meteor.isClient
Template.projectsIndex.projects = Projects.find()
Run Code Online (Sandbox Code Playgroud)
删除后,我还要另外做:
if Meteor.isServer
Meteor.publish "projects", ->
Projects.find()
if Meteor.isClient
Meteor.subscribe "projects"
Template.projectsIndex.projects = Projects.find()
Run Code Online (Sandbox Code Playgroud)
那么,说客户端find()方法只搜索从服务器端发布的记录是否准确?它一直在绊倒我,因为我觉得我应该只打电话find()一次.
使用Meteor,我想知道如何最好地处理共享相同服务器端数据库集合的不同客户端集合.考虑以下示例:我有一个User集合,在我的客户端,我有一个朋友用户列表,我有一个搜索功能,对整个用户数据库执行查询,返回与查询匹配的用户名列表.
在发布服务器端方法上,我对同一个集合有两个查询,它们返回不同的文档集.这些数据应该在客户端分成两个独立的集合吗?或者,与两个查询匹配的所有用户文档是否最终都在同一个集合中?如果是后者,我会重复用于服务器端和客户端查询的代码吗?
在服务器上:
Meteor.publish('searchResults', function(query){
var re = new RegExp(query, 'i')
return Users.find({ 'name' : {$regex: re}})
})
Run Code Online (Sandbox Code Playgroud)
在客户端:
Session.set('searchQuery', null)
Meteor.autosubscribe(function(){
Meteor.subscribe('searchResults', Session.get('searchQuery'))
})
Template.search.events = {
'keyup #user-search' : function(e){
Session.set('searchQuery', e.target.value)
}
}
_.extend(Template.search, {
searchResults: function() {
var re = new RegExp(Session.get('searchQuery'), 'i')
return Users.find({ 'name' : {$regex: re}})
}
})
Run Code Online (Sandbox Code Playgroud)
这似乎是一个看似合理的解决方案,但不是最佳解决方案.如果我想创建一个新的客户端集合,该集合由来自多个服务器端集合的搜索结果组成,该怎么办?
我有一个名为Profiles的服务器端mongo集合.
如果用户:adminId,我需要发布和订阅整个Profiles集合.
这样管理员就可以编辑,更新等...每个Profile集合项.
但我希望用户能够看到他们的个人资料记录.
所以我试过这个......
客户端
MyProfile = new Meteor.Collection("myprofile");
Meteor.subscribe('profiles');
Meteor.subscribe('myprofile');
Run Code Online (Sandbox Code Playgroud)
通用 - 客户端和服务器端
Profiles = new Meteor.Collection("profiles");
Run Code Online (Sandbox Code Playgroud)
服务器端 - 配置文件的发布和订阅工作正常.
// this returns all profiles for this User
// if they belong to an ACL Group that has acl_group_fetch rights
Meteor.publish("profiles", function() {
var user_groups = Groups.find({users: this.userId()});
var user_groups_selector = [];
user_groups.forEach(function (group) {
user_groups_selector.push(group._id);
});
return Profiles.find( {
acl_group_fetch: {
$in: user_groups_selector
}
});
});
Run Code Online (Sandbox Code Playgroud)
这是问题似乎开始的地方.Profiles.find返回集合项,因为我可以将它们输出到控制台服务器端.但由于某种原因,发布和订阅无效.客户端什么都没收到.
// return just the users profile as myprofile
Meteor.publish("myprofile", function() …Run Code Online (Sandbox Code Playgroud) 我现在已经把头撞到了墙上一段时间了,我想我在这里错过了一些简单的东西.
我在我的Meteor服务器上运行它:
// --- Collections ---
Projects = new Meteor.Collection('projects');
Team = new Meteor.Collection('team');
// --- Only publish user data for users on my team ---
Meteor.publish('team', function() {
var team = Meteor.users.findOne({_id: this.userId}).profile._team;
console.log(Meteor.users.find({'profile._team': team}, {fields: {_id: 1, profile: 1}}).fetch());
return Meteor.users.find({'profile._team': team}, {fields: {_id: 1, profile: 1}});
});
Run Code Online (Sandbox Code Playgroud)
通过在profile._team属性中与当前登录用户具有相同ID的所有用户文档上运行查询,可以查找位于同一"团队"的所有用户.您将console.log(...);在发布函数中看到(在return语句之前的行),并且它正确地将我期望它的文档记录到终端中.
现在我在我的客户端上运行它:
// --- Data ---
Meteor.subscribe('team');
Team = new Meteor.Collection('team');
Template.team.team = function() {
console.log(Team.findOne());
return Team.find();
};
Run Code Online (Sandbox Code Playgroud)
但是,console.log(Team.findOne())始终记录undefined,Team.find()始终返回一个空数组.我做错了什么是阻止我的文件到达客户端?
更新: 这是模板代码. …