【问题标题】:can not get current user after page refresh in Meteor.js在 Meteor.js 中刷新页面后无法获取当前用户
【发布时间】:2017-05-03 22:22:42
【问题描述】:

我正在尝试显示登录用户名。 登录后我可以得到用户名,但是一旦我刷新页面它是未定义的。

我的代码:

<p>{{currentUser.profile.full_name}}</p>

我也试试别的方法

火焰面

{{当前用户}}

js
Template.main.helpers({
  "currentUser": function() {
     if (Meteor.user())
         return Meteor.user().profile.full_name;
   }
});

登录后我可以获取名称,但刷新页面后我无法获取名称。

那么,如果我在 Meteor blaze 中刷新页面,获取名称的解决方案是什么?

【问题讨论】:

  • 页面刷新后你还在登录吗?如果你去浏览器控制台打印出 Meteor.userId(),它有值吗?我可以看到您的原始 html 没有任何问题。
  • 请说明您使用了哪些登录功能?谷歌、脸书或领英?

标签: meteor meteor-blaze


【解决方案1】:

试试这个:

  Template.Default.onCreated(function() {
    this.user = Meteor.user();
  });


  Template.main.helpers({
    currentUser: function() {
      var user = Template.instance().user;
      if (user) {
         return user.profile.full_name;
      }
  });

还要确保profile.full_name 存在;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2022-11-29
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    相关资源
    最近更新 更多