我正在尝试设置一种方法来向撇号事件模块添加注册人列表。我能够在模块中添加一个新的joinByMany字段,但我意识到我还需要存储注册日期并计算每个注册者对象。为了适应这种情况,我添加了以下字段,包括joinByOne用户数组,注册日期字符串和注册计数整数:
addFields: [
{
name: '_registrants',
label: 'Registrants',
type: 'array',
titleField: '_user.firstName',
schema: [
{
name: '_user',
withType: 'apostrophe-user',
type: 'joinByOne',
idField: 'userId',
filters: {
// Fetch just enough information
projection: {
id: 1,
username: 1,
firstName: 1,
lastName: 1,
email: 1
}
}
},
{
name: 'registrationDate',
label: 'Registration Date',
contextual: true,
type: 'string'
},
{
name: 'attendeeCount',
label: 'Total Attendees',
type: 'integer'
}
]
}
]
现在,我遇到的问题是我想在我的撇号事件页面show.html模板上显示“注册”或“取消注册”按钮,具体取决于当前是否登录用户在_registrants数组中。以前,当我使用joinByMany设置用户列表时,我可以在JoinByMany的idsField中查找当前用户id的索引。但是,现在,我必须以某种方式遍历_registrants并检查每个项_user.userId以检查相同的内容,我不知道该怎么做。我猜它会在模板中出现某种类型的逻辑,但我不知道如何在nunjucks中实现这一点。
当我使用joinByMany而不是joinByOnes数组时,我目前需要检查用户:
{% if data.piece.userIds.indexOf(data.user._id) < 0 %}
<div class="event-controls-item">
{{ buttons.major('Register for Field Trip', { action: 'register-event' }) }}
</div>
{% else %}
<div class="event-controls-item">
{{ buttons.danger('Unregister for Trip', { action: 'unregister-event' }) }}
</div>
{% endif %}
谢谢!
答案 0 :(得分:1)
您可以使用apos.utils.find
:
{% set registrant = apos.utils.find(data.piece._registrants, '_id', data.user._id) %}
{% if registrant %}
...
{% else %}
...
{% endif %}