使用express/ejs/mongo 仅显示一项

发布于 2025-01-18 18:54:48 字数 1354 浏览 1 评论 0原文

我一直在尝试制作一个非常简单的待办事项列表,但是当我想显示任务的描述时,它会显示所有描述,而不仅仅是该任务本身。看图片,我只想要每个任务的一个描述。 发生了什么

我正在使用 forEach 循环,但不知道是否应该创建一个新变量仅接收任务的描述。我可以做什么来仅显示描述中的一项,即参考任务。

这是 EJS 文件:

<% todo.forEach(function(task) { %>
    <div class="itens">
        <li>
            <a class="item" href="/edit/<%= task._id %>">
                <%= task.task %>
            </a>
            <a class="item del-btn" href="/delete/<%= task._id %>" onclick="return confirm('Deseja excluir?');">
                <img class="trash-icon" src="/images/trash.png" alt="">
            </a>
            <a class="item del-btn" href="/delete/<%= task._id %>" onclick="return confirm('Tarefa Concluida?');">
                <img class="trash-icon" src="/images/done.png" alt="">
            </a>
            <% todo.forEach(function(desc) { %>
                <p>
                    <%= desc.desc %>
                </p>
            <% }); %>
        </li>
    </div>
<% }); %>

嘿,我弄清楚这里出了什么问题。第二个循环使描述显示重复,而不仅仅是针对任务。 只需删除第二个循环并将 <%= desc.desc %> 更改为 <%= task.desc %> 并且可以正常工作!

I've been trying to make a very simple ToDo list but when I want to show the description of the tasks, it displays all the descriptions, not only for that task itself. See the image, I want only one description by each task.
Whats happening

I'm using a forEach loop but idk if I should create a new variable to receive only the description of tasks. What can I do to show only one item from description and that's refer to the task.

Here is the EJS file:

<% todo.forEach(function(task) { %>
    <div class="itens">
        <li>
            <a class="item" href="/edit/<%= task._id %>">
                <%= task.task %>
            </a>
            <a class="item del-btn" href="/delete/<%= task._id %>" onclick="return confirm('Deseja excluir?');">
                <img class="trash-icon" src="/images/trash.png" alt="">
            </a>
            <a class="item del-btn" href="/delete/<%= task._id %>" onclick="return confirm('Tarefa Concluida?');">
                <img class="trash-icon" src="/images/done.png" alt="">
            </a>
            <% todo.forEach(function(desc) { %>
                <p>
                    <%= desc.desc %>
                </p>
            <% }); %>
        </li>
    </div>
<% }); %>

Hey, I figure out what was the problem here. The second loop is what makes the description show duplicated and not only for the task.
Just erase the second loop and changes the <%= desc.desc %> to
<%= task.desc %> and will work fine!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文