gpt4 book ai didi

javascript - meteor JS : RegisterHelper to display associated user information

转载 作者:行者123 更新时间:2023-12-03 12:45:37 25 4
gpt4 key购买 nike

我正在开发一个小型 MeteorJS 应用程序(供个人使用),它将是一个非常基本的 CMS。

我想在每个帖子旁边显示用户信息(创建帖子的用户)。

这是我的 Handlebars (或空格键)模板:

<template name="post">
<div class="row">
<div class="col-md-7">
<h3>{{ title }} - <span class="time" title="{{createdAt}}">{{createdAt}}</span></h3>
<p>Created By - {{ user_id }}</p>
{{{content}}}
</div>

<div class="col-md-5">
{{> editActions }}
</div>
</div>
</template>

<template name="editActions">
<ul class="nav nav-pills">
<li>
<a href="{{ pathFor 'postEdit' }}">
<i class="glyphicon glyphicon-edit"></i>
</a>
</li>
<li>
<a href="{{ pathFor 'postRemove' }}" data-id="{{ _id }}" class="remove-post">
<i class="glyphicon glyphicon-remove"></i>
</a>
</li>
</ul>
</template>

我显示了 {{ user_id }},但我不确定如何显示实际的用户电子邮件(或与此相关的任何其他用户信息)。

user_id 是我的Posts 集合中的一个字段。

最佳答案

{{user_id}} 替换为 {{authorEmail}},然后添加一个模板助手,如下所示:

Template.post.helpers({
authorEmail: function() {
return Meteor.users.findOne(this.user_id).emails[0].address;
}
});
<小时/>

如果您想直接访问模板中的作者,您可以从助手返回用户文档:

Template.post.helpers({
author: function() {
return Meteor.users.findOne(this.user_id);
}
});

然后像这样使用它:

<div class="col-md-7">
<h3>{{ title }} - <span class="time" title="{{createdAt}}">{{createdAt}}</span></h3>
{{#with author}}
<p>Created By - {{profile.name}}</p>
{{/with}}
{{{content}}}
</div>

关于javascript - meteor JS : RegisterHelper to display associated user information,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23354318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com