簡體   English   中英

Meteor.user()。profile返回'profile'未定義

[英]Meteor.user().profile returns 'profile' undefined

我有一個名為'isActive'的幫助器和一個名為'create'的模板..見下文

Template.create.isActive = function () {
  return Meteor.user().profile.isActive;
};

當我嘗試運行此代碼時,它在控制台中返回以下內容:“模板助手中的異常:TypeError:無法讀取未定義的屬性'profile'”。

基本上我想從當前用戶配置文件中提取'isActive'信息並將其返回到模板。 知道為什么這不起作用嗎?

更新

//startup on server side:
Meteor.publish("userData", function() {
  if (this.userId) {
    return Meteor.users.find({_id: this.userId},
      {fields: {'profile.isActive': 1}});
  } else {
    this.ready();
  }
});

//startup on client side
Meteor.subscribe('userData');

//router
this.route('list', {
  path: 'list',
  waitOn : function () {
    return Meteor.subscribe('userData');
  },
  data : function () {
    return Meteor.users.findOne({_id: this.params._id});
  },
  action : function () {
    if (this.ready()) {
      this.render();
    }
  }
});

Meteor.user()未定義。 您未使用任何用戶登錄,或者在將用戶集合同步到客戶端之前呈現模板。 如果您使用像鐵路由器這樣的路由器,您可以等待可用的收集或用戶登錄。

不使用路由器,最簡單的方法是檢查用戶是否已定義:

Template.create.isActive = function () {
    return Meteor.user() && Meteor.user().profile.isActive;
}

由於Meteor.user()具有反應性,因此當用戶更改時(即可用時)將重新呈現模板。

這是一個常見的錯誤,請參閱:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM