我正在创建一个跟踪项目和报告的网站。我的目标是创建一个前端端点 (http://localhost:3001/post/1),以显示与给定项目关联的所有报告。源代码可以在这里找到:https://github.com/CullerWhale/projectTracker
网站使用sequelize、mysql2、express做handlebars和session。一个用户可以有多个项目,一个项目可以有多个报表。所有报告都属于一个项目,所有项目都属于一个用户。
仅供参考:项目在代码中称为帖子,我会在最后解决这个问题,我只想继续前进。
我能够正确显示给定登录用户的所有报告:
我使用 dashboard.handlebars 文件来执行此操作。它还包括使用 post-info.handlebars 部分文件以及 post-routes.js 文件。
然后我创建了一个新模型:报告。我在 routes/api 目录的 index.js 文件中添加了关联。我重新加载了表格并为报告创建了一个新的部分:report-info.handlebars
我做了相同的过程来创建仪表板以显示一个用户的所有报告。但是,它不显示给定项目的报告:
这里是 single-post.handlebars 页面的代码:
{{> post-info post }}
{{#if loggedIn}}
<div>
<button type="submit">Edit project</button>
<button type="button" class="upvote-btn">Delete project</button>
</div>
{{/if}}
{{#if post.reports.length}}
<section>
<h2>Reports for this post</h2>
<ol>
{{#each post.reports as |report|}}
<li>
<div class="report-content">
{{> report-info report}}
</div>
<br />
<div class="edit-link-container">
<a href="/dashboard/edit/{{report.id}}" class="edit-link">Edit report</a>
</div>
</li>
{{/each}}
</ol>
{{else}}
<p>No reports found for this post.</p>
</section>
{{/if}}
<section>
<h2>Create New Report</h2>
<form class="new-post-form">
<div>
<label for="post-title">Report Name</label>
<input type="text" id="post-title" name="post-title" />
</div>
<div>
<label for="post-url">Previous report</label>
<input id="post-url" name="post-url" />
</div>
{{!-- {{#if loggedIn}} --}}
<div>
<button type="submit">Generate new report</button>
{{!-- <button type="button" class="upvote-btn">Create new report</button> --}}
</div>
</form>
{{!-- {{/if}} --}}
</section>
<script src="/javascript/add-post.js"></script>
这里是部分文件... post-info.handlebars:
<article class="post">
<div class="title">
<a href="/post/{{id}}">{{title}}</a>
<span>({{post_url}})</span>
</div>
<div class="meta">
by {{user.username}} on
{{created_at}}
<a href="/post/{{id}}"></a>
</div>
report-info.handlebars:
<article class="report">
<div class="title">
<a href="{{post.post_url}}" target="_blank">{{title}}</a>
</div>
<div class="post-url">
<span>{{post.post_url}}</span>
</div>
<div class="meta">
<span>by {{user.username}}</span>
</div>
我已经尝试了无数的东西,但我就是无法让它工作。非常感谢任何帮助。
我在上面睡觉后想出来了。我专注于 post-routes.js 文件并使用多个部分。但是,我应该关注 report-routes.js 文件,添加查询以查找给定帖子的所有报告的适当位置。