如何使Meteor.user字段反应 [英] how to make Meteor.user field reactive

查看:100
本文介绍了如何使Meteor.user字段反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图阻止打开多个浏览器的用户登录.

当用户注册时,我有一个Meteor.user()对象填充如下:

I have a Meteor.user() object populated as follows when a user signs up:

{
    "_id" : "uSS2RqZnnFwui67wk",
    "createdAt" : ISODate("2017-05-15T07:28:10.546Z"),
    "services" : {
        "password" : {
            "bcrypt" : "$2a$10$DPgA59Gmob4ajzjYZyh5auoHRUyQuF1/7M0KaWz.nzW0mIEqzlDK6"
        },
        "resume" : {
            "loginTokens" : [ 
                {
                    "when" : ISODate("2017-05-15T13:42:29.322Z"),
                    "hashedToken" : "tkoQnweSQhgRKGzaJTAkUU3/Ljd3p4wrBJfrRvRRlcY="
                }
            ]
        }
    },
    "username" : "johndoe",
    "emails" : [ 
        {
            "address" : "lkj@gmail.com",
            "verified" : false
        }
    ],
    "profile" : {
        "name" : "John Doe",
        "mobile" : "9637637941",
        "email" : "lkj@gmail.com",
        "address" : "kfasd, asdas,d as dsad",
        "gender" : "M",
        "state" : "Uttar Pradesh",
        "customerType" : "CLIENT",
        "isBlocked" : true
    },
    "status" : {
        "online" : true,
        "lastLogin" : {
            "date" : ISODate("2017-05-15T14:12:02.094Z"),
            "ipAddr" : "127.0.0.1",
            "userAgent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0"
        },
        "idle" : false
    }
}

参考上面的代码,我试图根据user.profile.isBlocked*状态更新UI.

Referring to the above code, I am trying to update a UI based on the user.profile.isBlocked* status.

我的 UI.html 如下:

<template name="App_watch">
    {{#if isBlocked}}
      User Has been Blocked.
    {{else}}
      User has Access.
    {{/if}}
</template>

我的 UI.js 如下:

import { Meteor } from 'meteor/meteor';
import './UI.html';

Template.App_watch.helpers({
  isBlocked() {
    user = Meteor.users.find({_id: Meteor.userId});
    return user.profile.isBlocked;
  }
});

在下面的代码中,我只是在监视是否以同一登录打开了多个浏览器.如果是,则阻止用户,否则取消阻止用户.

In the code below I am simply monitoring whether there are more than 1 browsers open with same log in. If YES then block the user, else Unblock the user.

import './fixtures.js';
import './register-api.js';

UserStatus.events.on("connectionLogin", function(fields) {
  var count = UserStatus.connections.find({userId : fields.userId}).count();
  if(count > 1) { //Block
    Meteor.users.update({_id: Meteor.userId()}, {$set: {"profile.isBlocked": true}});
  } else { // Unblock
    Meteor.users.update({_id: Meteor.userId()}, {$set: {"profile.isBlocked": false}});
  }
});

问题陈述:

我想使isBlocked变量具有反应性,例如当isBlocked标志为用户更改时.当前它是静态的,需要刷新.

I want to make the isBlocked variable reactive as an when the isBlocked flag changes for the user. Currently it is static and needs refresh.

推荐答案

尝试:

Template.App_watch.helpers({
  isBlocked() {
    return Meteor.user() && Meteor.user().profile && Meteor.user().profile.isBlocked;
  }
});

如果要查找单个对象,则需要使用.findOne()而不是.find(),因为后者会返回光标.也是Meteor.userId()而不是Meteor.userId

If you're looking for a single object you need to use .findOne() instead of .find() as the latter returns a cursor. It's also Meteor.userId() not Meteor.userId

这篇关于如何使Meteor.user字段反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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