简体   繁体   English

显示Meteor.user()

[英]Displaying Meteor.user()

I have this template: 我有这个模板:

<template name="index">
  {{#if currentUser}
    {{userid}}
  {{/if}}
</template>

What is the difference between 之间有什么区别

Template.index.helpers({
    userid: function() {
        return Meteor.user()._id;
    }
});

AND

Template.index.helpers({
    userid: Meteor.user()._id
});

The last one gives this error: Uncaught TypeError: Cannot read property '_id' of undefined 最后一个给出此错误: Uncaught TypeError:无法读取未定义的属性'_id'

The easiest thing to do is use {{currentUser._id}} 最简单的方法是使用{{currentUser._id}}

Meteor.user()._id is correct. Meteor.user()._id是正确的。 The problem is when you load the page, initially the data is not available and Meteor.user() will be null until a DDP connection is established and your browser logs you in (this takes a few hundred milliseconds). 问题是当您加载页面时,最初数据不可用, Meteor.user()将为空,直到建立DDP连接并且浏览器将您登录为止(这需要几百毫秒)。 Meteor.user() && Meteor.user()._id to correct this. Meteor.user() && Meteor.user()._id来更正此问题。

The difference between the two Meteor.user()._id is in one you are passing the first static value that loads when the template is loaded. 这两个Meteor.user()._id之间的区别在于,您传递的是在加载模板时加载的第一个静态值。 It will stick to being that even if Meteor.user() changes, or you log out. 即使Meteor.user()更改或您注销,它也将坚持下去。

When you pass a function you tell the helper it can be recalculated and it will update if there are any reactive changed. 传递function您告诉助手可以重新计算,并且如果有任何反应式更改,它将更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM