简体   繁体   English

流星:服务器和客户端上的用户数不同

[英]Meteor: Different user count on server and client

Why do I get different results for all users on client and server side? 为什么客户端和服务器端的所有用户得到不同的结果? On client side it is always 1. Does it matter if I'm logged in? 在客户端,它始终为1。是否已登录是否重要? And if this is the case, how can I get all data while I'm logged in? 如果是这种情况,我如何在登录时获取所有数据?

Console Server: Users.find().count() = 7
Console Client: Users.find().count() = 1

shared/collections.js 共享/ collections.js

Users = Meteor.users;

client/router.js 客户机/ router.js

Router.route('/users', {
    name: 'users',
    data: function() {
         return {
            usersAll: Users.find({})
         }
    }
});

template 模板

<template name="users">
    <ul class="list">
        {{#each usersAll}}
            <li><a href="/user/{{_id}}">{{profile.name}}</a></li>
        {{/each}}
    </ul>
</template>

In the template I just get one result. 在模板中,我只会得到一个结果。

Yes, it does matter that you are logged in. You have to publish and subscribe to data on the client. 是的,登录确实很重要。您必须在客户端上发布和订阅数据。 If you attempt to do Meteor.users.find() on the client, you will always return the currently active user. 如果尝试在客户端上执行Meteor.users.find() ,则将始终返回当前活动的用户。 (if any) (如果有)

If you do Meteor.users.find() on the server you will return ALL users, regardless of the publication/subscription. 如果在服务器上执行Meteor.users.find() ,则将返回所有用户,无论发布/订阅如何。 It's a security precaution, to keep regular users from accessing private information on the client. 这是一项安全预防措施,可以防止普通用户访问客户端上的私人信息。

You can publish more or less data to the client if you choose. 您可以选择将更多或更少的数据发布到客户端。 Check this article out: https://www.meteor.com/tutorials/blaze/publish-and-subscribe 查阅本文: https : //www.meteor.com/tutorials/blaze/publish-and-subscribe

Or the documentation: http://docs.meteor.com/#/full/meteor_publish 或文档: http//docs.meteor.com/#/full/meteor_publish

You can always add the autopublish package if you want to publish all data. 如果要发布所有数据,则始终可以添加autopublish发布程序包。 This is not recommended for production apps though. 但是,不建议将其用于生产应用程序。

Server 服务器

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

Client 客户

Template.users.onCreated(function(){
    var instance = this;

    instance.autorun(function(){
         var allUsers = instance.subscribe('allUsers');
    });
});

Template.users.helpers({
    'usersAll': function(){
        return Meteor.users.find().fetch();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM