how to make Meteor.user field reactive

感情迁移 提交于 2019-12-25 07:40:16

问题


I am trying to block user who open more than one browser being logged In.

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
    }
}

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

My UI.html is as below:

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

My UI.js is as below:

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}});
  }
});

Problem Statement:

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.


回答1:


Try:

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

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



来源:https://stackoverflow.com/questions/43982528/how-to-make-meteor-user-field-reactive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!