fetch comments separately from article
This commit is contained in:
parent
fea8247bc4
commit
e3f6fe0d2e
@ -9,9 +9,6 @@ export async function get(req, res, next) {
|
|||||||
select: 'realname'
|
select: 'realname'
|
||||||
}).populate({
|
}).populate({
|
||||||
path: 'category'
|
path: 'category'
|
||||||
}).populate({
|
|
||||||
path: 'comments.author_user',
|
|
||||||
select: 'realname'
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (article) {
|
if (article) {
|
||||||
|
@ -2,13 +2,18 @@
|
|||||||
export async function preload({ params, query }) {
|
export async function preload({ params, query }) {
|
||||||
// the `slug` parameter is available because
|
// the `slug` parameter is available because
|
||||||
// this file is called [slug].svelte
|
// this file is called [slug].svelte
|
||||||
const res = await this.fetch(`a/${params.slug}.json`);
|
const articleRes = await this.fetch(`a/${params.slug}.json`);
|
||||||
const data = await res.json();
|
const article = await articleRes.json();
|
||||||
|
|
||||||
if (res.status === 200) {
|
const commentsRes = await this.fetch(`a/${params.slug}/comments`);
|
||||||
return { article: data };
|
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 {
|
} else {
|
||||||
this.error(res.status, data.message);
|
this.error(commentsRes.status, comments.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@ -17,12 +22,13 @@
|
|||||||
import { stores } from '@sapper/app';
|
import { stores } from '@sapper/app';
|
||||||
const { session } = stores();
|
const { session } = stores();
|
||||||
export let article;
|
export let article;
|
||||||
|
export let comments;
|
||||||
|
|
||||||
let author = '', content = '';
|
let author = '', content = '';
|
||||||
|
|
||||||
async function postComment()
|
async function postComment()
|
||||||
{
|
{
|
||||||
const res = await fetch(`/a/${article.slug}/comment`, {
|
const res = await fetch(`/a/${article.slug}/comments`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
@ -33,8 +39,9 @@
|
|||||||
const json = await res.json();
|
const json = await res.json();
|
||||||
if (json.message) {
|
if (json.message) {
|
||||||
alert(json.message);
|
alert(json.message);
|
||||||
} else if (Array.isArray(json)) {
|
} else {
|
||||||
article.comments = json;
|
const res = await fetch(`/a/${article.slug}/comments`);
|
||||||
|
comments = await res.json();
|
||||||
author = content = '';
|
author = content = '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -159,7 +166,7 @@
|
|||||||
<hr>
|
<hr>
|
||||||
<h3>Comments</h3>
|
<h3>Comments</h3>
|
||||||
<div class="comments">
|
<div class="comments">
|
||||||
{#each article.comments as comment}
|
{#each comments as comment}
|
||||||
<div class="comment">
|
<div class="comment">
|
||||||
<p class="comment-meta">
|
<p class="comment-meta">
|
||||||
{#if comment.author_user}
|
{#if comment.author_user}
|
||||||
@ -175,7 +182,7 @@
|
|||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
<h3>Add a Comment</h3>
|
<h3>Add a Comment</h3>
|
||||||
<form method="POST" action={`/a/${article.slug}/comment`}>
|
<form method="POST" action={`/a/${article.slug}/comments`}>
|
||||||
<p>Name:
|
<p>Name:
|
||||||
{#if $session.user}
|
{#if $session.user}
|
||||||
<input type="text" disabled value={$session.user.realname}>
|
<input type="text" disabled value={$session.user.realname}>
|
||||||
|
@ -1,8 +1,29 @@
|
|||||||
import Article from '../../../models/article.js';
|
import Article from '../../../models/article.js';
|
||||||
|
|
||||||
|
export async function get(req, res) {
|
||||||
|
const { slug } = req.params;
|
||||||
|
const article = await Article.findOne({ slug }).populate({
|
||||||
|
path: 'comments.author_user',
|
||||||
|
select: 'realname'
|
||||||
|
});
|
||||||
|
if (article) {
|
||||||
|
res.writeHead(200, {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
});
|
||||||
|
res.end(JSON.stringify(article.comments));
|
||||||
|
} else {
|
||||||
|
res.writeHead(404, {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
});
|
||||||
|
res.end(JSON.stringify({
|
||||||
|
message: `Not found`
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function post(req, res) {
|
export async function post(req, res) {
|
||||||
const { slug } = req.params;
|
const { slug } = req.params;
|
||||||
const article = await Article.findOne({ slug }).populate({
|
let article = await Article.findOne({ slug }).populate({
|
||||||
path: 'comments.author_user',
|
path: 'comments.author_user',
|
||||||
select: 'realname'
|
select: 'realname'
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user