diff --git a/src/routes/a/[slug].json.js b/src/routes/a/[slug].json.js index dd14603..48a706e 100644 --- a/src/routes/a/[slug].json.js +++ b/src/routes/a/[slug].json.js @@ -9,9 +9,6 @@ export async function get(req, res, next) { select: 'realname' }).populate({ path: 'category' - }).populate({ - path: 'comments.author_user', - select: 'realname' }); if (article) { diff --git a/src/routes/a/[slug].svelte b/src/routes/a/[slug].svelte index 6543848..70fc58e 100644 --- a/src/routes/a/[slug].svelte +++ b/src/routes/a/[slug].svelte @@ -2,13 +2,18 @@ export async function preload({ params, query }) { // the `slug` parameter is available because // this file is called [slug].svelte - const res = await this.fetch(`a/${params.slug}.json`); - const data = await res.json(); + const articleRes = await this.fetch(`a/${params.slug}.json`); + const article = await articleRes.json(); - if (res.status === 200) { - return { article: data }; + const commentsRes = await this.fetch(`a/${params.slug}/comments`); + const comments = await commentsRes.json(); + + if (articleRes.status === 200 && commentsRes.status === 200) { + return { article, comments }; + } else if (articleRes.status !== 200) { + this.error(articleRes.status, article.message); } else { - this.error(res.status, data.message); + this.error(commentsRes.status, comments.message); } } @@ -17,12 +22,13 @@ import { stores } from '@sapper/app'; const { session } = stores(); export let article; + export let comments; let author = '', content = ''; async function postComment() { - const res = await fetch(`/a/${article.slug}/comment`, { + const res = await fetch(`/a/${article.slug}/comments`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -33,8 +39,9 @@ const json = await res.json(); if (json.message) { alert(json.message); - } else if (Array.isArray(json)) { - article.comments = json; + } else { + const res = await fetch(`/a/${article.slug}/comments`); + comments = await res.json(); author = content = ''; } } @@ -159,7 +166,7 @@
{#if comment.author_user} @@ -175,7 +182,7 @@ {/each}
Add a Comment
-