Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor.users发布和订阅不匹配_Meteor - Fatal编程技术网

Meteor.users发布和订阅不匹配

Meteor.users发布和订阅不匹配,meteor,Meteor,我已从我的Meteor应用程序中删除了自动发布包,并在我的服务器上创建了一个发布: Meteor.publish("userData", function () { return Meteor.users.find( {_id: this.userId}, {fields: {'profile': 0}} ); }); 正如您在上面看到的,我已经将配置文件设置为0,这意味着我想排除它。然而 在我的客户机上,我有以下代码:

我已从我的Meteor应用程序中删除了
自动发布
包,并在我的服务器上创建了一个发布:

Meteor.publish("userData", function () {
    return Meteor.users.find(
            {_id: this.userId},
            {fields: {'profile': 0}}
        );
});
正如您在上面看到的,我已经将
配置文件设置为
0
,这意味着我想排除它。然而

在我的客户机上,我有以下代码:

Meteor.subscribe("userData", (param) => {
      console.log( Meteor.users.find(Meteor.userId()).fetch() )
    })
并且输出仍然包括配置文件:

createdAt: Sat May 19 2018 11:16:25 GMT+0800 (+08) {}
emails: [{…}]
profile: {name: "Second Second"}
services: {password: {…}, resume: {…}}
username: "seconduser"
_id: "ESmRokNscFcBA9yN4"
__proto__: Object
length: 1
__proto__: Array(0)

原因是什么?

可能有其他人订阅了用户的
配置文件
字段

您可以通过查看通过websocket发送的信息来了解情况

  • 打开调试器
  • 查找“网络”选项卡
  • 找到websocket连接
  • 查找内容或“框架”

  • 在那里,您可以看到制作了哪些
    sub
    ,以及服务器发布到
    集合的哪些更新。看看没有你的潜艇会是什么样子;可能用户文档已经发布。

    首先,请确定是要使用
    Meteor.subscribe()
    还是要使用
    this.subscribe()
    。他们之间有很大的不同

  • Meteor.subscribe()
    在屏幕/路由/UI之间切换时,将保持订阅不受影响
  • this.subscribe()
    将具有订阅范围,直到
    模板的生命周期存在。当您切换到其他路由/路径/UI时,订阅将被销毁。当您在屏幕的连续转换中有多种订阅,并且尽管在集合查询中进行了筛选,但UI中显示的不需要的数据仍会出现问题时,在特定情况下使用此选项
  • 欲了解更多信息

    针对您的确切问题,当Meteor知道您是一个有效的登录用户时,它会在UI上发送整个用户特定的收集字段
    \u id
    电子邮件
    配置文件
    用户名
    。因此,建议您只将所需的数据放入用户集合中。无论您是否对自己的数据进行特殊的订阅,您都将始终能够在UI上访问自己的数据,即使是在生产构建中。您可以通过放置
    console.log(Meteor.user())进行检查在chrome控制台中。这就是Meteor.user()的制作方法,不管你喜欢与否。MDG(Meteor Development Group)假设,当用户登录时,用户可以在UI上完全访问他/她自己的数据,因为这是安全有效的

    参考下图


    您可以在客户端上看到
    profile
    字段,因为您在创建或更新用户时已经编辑并启用了
    用户
    对象的此特殊字段。为了保护此对象不受客户端修改的影响,可以使用以下服务器端代码拒绝来自客户端的所有写入

    // Deny all client-side updates to user documents
    Meteor.users.deny({
        update () { return true; },
    });
    
    因此,即使客户机上的
    profile
    字段在
    Meteor.user()
    对象中可用,客户机也无法进行任何修改

    Meteor.publish("userData", function () {
        console.log('publishing userData with id', this.userId);
        return Meteor.users.find(
                {_id: this.userId},
                {fields: {'customProfile': 0}}
            );
    });
    
    如果您发布的是自定义数据,那么您可以按照自己的方式控制其显示。例如,假设我们在用户对象中引入了一个新字段
    customProfile
    ,而不是使用以下代码,客户机将看不到
    customProfile

    Meteor.publish("userData", function () {
        console.log('publishing userData with id', this.userId);
        return Meteor.users.find(
                {_id: this.userId},
                {fields: {'customProfile': 0}}
            );
    });
    

    您可以在中找到更多信息。

    Meteor自动发布当前用户文档的某些字段。其中一个字段是
    配置文件
    。请注意,不应在生产系统中发布您所做的数据(密码哈希等)。我假设您这样做只是为了测试目的,但我提到它是为了避免任何人错误地这样做。Meteor不会自动将整个用户文档发布给当前用户,但始终会发布
    配置文件
    键。