From 0362d04a5e16510f320e91b629ea2c9baf5c8afa Mon Sep 17 00:00:00 2001 From: Hoang Vo Date: Thu, 23 Sep 2021 02:22:56 +0700 Subject: [PATCH] Fix findPostById#withUser argument undefined --- api-lib/db/post.js | 8 +++----- pages/user/[username]/post/[postId].jsx | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/api-lib/db/post.js b/api-lib/db/post.js index 0a409cb..6e0dd29 100644 --- a/api-lib/db/post.js +++ b/api-lib/db/post.js @@ -4,11 +4,9 @@ import { dbProjectionUsers } from './user'; export async function findPostById(db, id) { const post = await db.collection('posts').findOne({ _id: id }); if (!post) return null; - if (withUser) { - post.creator = await db - .collection('users') - .findOne({ _id: post.creatorId }, { projection: dbProjectionUsers() }); - } + post.creator = await db + .collection('users') + .findOne({ _id: post.creatorId }, { projection: dbProjectionUsers() }); return post; } diff --git a/pages/user/[username]/post/[postId].jsx b/pages/user/[username]/post/[postId].jsx index 0f4f3fe..f8e6519 100644 --- a/pages/user/[username]/post/[postId].jsx +++ b/pages/user/[username]/post/[postId].jsx @@ -25,7 +25,7 @@ export default function UserPost({ post }) { export async function getServerSideProps(context) { await nc().use(database).run(context.req, context.res); - const post = await findPostById(context.req.db, context.params.postId); + const post = await findPostById(context.req.db, context.params.postId, true); if (!post) { return { notFound: true,