MeteorJS:在客户端获取用户配置文件 [英] MeteorJS: Get user profile on client side

查看:53
本文介绍了MeteorJS:在客户端获取用户配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这个问题已经被很好地记录在案,但我在这里,又卡住了......

So, this problem has been well documented, yet here I am, stuck again...

在服务器端,我已经发布:

On server side, I have publish:

Meteor.publish('userData', function(){ if (!this.userId) { return null; } 
return Meteor.users.find({}, {fields: {'profile': 1}}); });

在客户端路由器订阅方面,我有:

On client side router subscriptions, I have:

this.register('userData', Meteor.subscribe('userData'));

然后在我的客户端代码中,我有:

And then in my client code, I have:

if (Meteor.userId()) {
  var profile = Meteor.users.find(Meteor.userId()).profile;
  console.log(profile); // I keep getting undefined...

我没有使用自动发布或不安全的包.

I am not using the autopublish or insecure package.

我在 mongodb 集合中的数据如下所示:

My data in the mongodb collection looks like:

{"_id" : "...", profile: { "password" : { "network" : "...", "picture" : "none" } }

我的错误说:

Uncaught TypeError: Cannot read property 'password' of undefined

想法?

推荐答案

所以,事实证明,我的大部分代码都是正确的,但是正如 ghybs 上面指出的,我仍然需要等待用户数据.所以,这是我的工作代码:

So, as it turns out, most of my code was correct, but as ghybs pointed out above, I still needed to wait for the user data. So, here is my working code:

// in publish.js on the server
Meteor.publish('userData', function(){ if (!this.userId) { return null; } 
return Meteor.users.find({_id: this.userId}, {fields: {'profile': 1}}); });

// in routes.jsx using FlowRouter
// after importing all the module external data
FlowRouter.route('/',{subscriptions:function(){
  this.register('getUser', Meteor.subscribe('userData'));
}, action() {
  mount(...etc...

// Keep in mind that I am using TrackerReact NPM to handle re-renders...
// in my component
if (FlowRouter.subsReady("getUser")) {
  // this returns an array with one object
  var userObject = Meteor.users.find({_id: Meteor.userId()}).fetch()[0];

这篇关于MeteorJS:在客户端获取用户配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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