Meteor:将所有用户发布到客户端 [英] Meteor: Publishing all users to the client

查看:57
本文介绍了Meteor:将所有用户发布到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这不起作用?:

在客户端和服务器上:

AllUsers = new Meteor.Collection('allUsers');

仅在服务器上:

Meteor.publish('allUsers', function() {
return Meteor.users.find();
});

仅在客户端:

Deps.autorun(function(){
Meteor.subscribe("allUsers");
});

在我启动此应用程序后,AllUsers.find().count() 为零,但在终端中执行 db.users.find.count() 会给出正确的数字 (3).即使我在浏览器中添加了一个新用户(使用标准的 ui-accounts 包形式),这肯定会导致用户集合发生变化,我的 AllUsers 集合中仍然没有文档.我正在努力解决这个问题!

After I start up this app, AllUsers.find().count() is zero, but doing db.users.find.count() in the terminal gives the correct number (3). Even after I add a new user in the browser (using the standard ui-accounts package form), which should certainly cause the users collection to change, I still have no documents in my AllUsers collection. I'm beating my head against the wall trying to solve this!

推荐答案

服务器上定义的 Meteor 集合与数据库中的集合具有 1:1 的关系.您在数据库中没有名为 'allusers' 的集合,因此该定义没有意义.您似乎混淆了数据库集合和已发布结果集的概念.

Meteor collections defined on the server have a 1:1 relationship with collections in the database. You don't have a collection in the DB called 'allusers', so that definition doesn't make sense. It seems like you are confusing the notion of a database collection and a published result set.

当您将帐户包添加到您的项目时,meteor 会在客户端和服务器上为您定义 Meteor.users 集合,因此您无需再次执行此操作.您的代码看起来不错 - 只需删除 new Meteor.Collection 并通过 Meteor.users.find 访问用户.

When you add the accounts package to your project, meteor defines the Meteor.users collection for you on both the client and the server, so you don't need to do that again. Your code looks fine - just remove the new Meteor.Collection and access the users via Meteor.users.find.

  • 示例:Meteor.usersaccounts 包创建
  • 注释:数据库中的集合是 db.users.您可以通过执行诸如 Users = Meteor.users 之类的操作,然后调用 Users.find() 而不是 Meteor,使这个集合看起来像您定义的其他集合.users.find().
  • example: Meteor.users is created by the accounts package
  • notes: The collection in the DB is db.users. You can make this collection look like the others you define by doing something like Users = Meteor.users, and later calling Users.find() instead of Meteor.users.find().
  • 示例:new Meteor.Collection('rooms')
  • 注释:在大多数项目中,这几乎是您一直在做的事情.字符串 'rooms' 是数据库中集合的名称 (db.rooms).文档可以从服务器发布到客户端.
  • example: new Meteor.Collection('rooms')
  • notes: In most projects, this is what you do nearly all of the time. The string 'rooms' is the name of the collection in the database (db.rooms). Documents can be published from the server to the client.
  • 示例:new Meteor.Collection('userCount')
  • 说明:服务器可以写入客户端集合,但不用于将数据同步到数据库.典型的用例是通知客户端有关另一个集合的大小或其他元数据.这里的字符串 'userCount' 并不对应于 DB 集合的名称,而只是客户端和服务器商定的标识符.
  • 另见:如何:发布到仅限客户端的集合Meteor 订阅和显示用户数
  • example: new Meteor.Collection('userCount')
  • notes: The server can write to the client collection, but it is not used to sync data to the DB. The typical use case is to inform the client with size or other metadata about another collection. Here the string 'userCount' does not correspond to the name of a DB collection, but instead is simply an identifier agreed upon by the client and server.
  • see also: How To: Publish to a Client Only Collection and Meteor Subscribe and Display Users Count
  • 示例:new Meteor.Collection(null)
  • 注释:这些是在客户端想要拥有集合语义,但不想使用集合与服务器通信时定义的.一个典型的用例是一个演示应用程序,用户可以在其中玩一个界面,但只能更改她自己浏览器中的数据.

这篇关于Meteor:将所有用户发布到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆