我正在从数据库(带有 Mongoose 的 MongoDB)中获取“评论”列表,每个评论都包含一组可以查看这些评论的允许用户。如何验证登录的用户是否在该数组中?
<% if(comment.allowed.length > 0 ){ %>
<% for(i = 0 ; i < comment.allowed.length; i++) { %>
<% if(comment.allowed[i] === auth) { %>
You already payed
<% } else { %>
<form action="/pay" method="POST">
<button class="button-upload">Pay $1</button>
</form>
<% } %>
<% } else { %>
<form action="/pay" method="POST">
<button class="button-upload">Pay $1</button>
</form>
<% } %>
<% } %>
我已经这样做了,但是在我的数组中我收到了 2 封电子邮件:email1@mail.com 和 email2@mail.com。登录的用户是 email1@mail.com,在我的页面中,我得到了“你已经付款”和带有按钮的表单,因为第一个值为 true,然后第二个值为 false。在我呈现页面的 .js 文件中,我可以使用 array.includes(value),但在这里我不能。我怎样才能只显示其中一种情况?还有许多其他评论,对于每一条评论,我都必须验证这一点,这就是为什么我不在我的 .js 文件中这样做的原因,我会忘记登录用户可以看到或看不到的评论。
是否有任何等效的 array.includes(value) 可以在我的 EJS 文件中使用,或者有任何解决方案来完成我想要的?先感谢您!