Using revalidatePath in server actions
Unanswered
Blue-throated Hummingbird posted this in #help-forum
Blue-throated HummingbirdOP
I am using a server action to create a document in my database, when I cal this function, creating a post, then use
Server Action:
How I'm using it:
revalidatePath('/')
and then navigate to '/' the data is not updated? Am I doing anything wrong?Server Action:
async function createPost(data: PostCreateData): Promise<PostType | { error: string }> {
dbConnect();
const user = await getUserFromSession();
if (!user) return { error: 'You must be logged in to create a post' };
if (!data.title || !data.description) return { error: 'You must provide a title and description' };
const post = await Post.create({
title: data.title,
tags: data.tags,
description: data.description,
publicPost: data.publicPost,
authorId: user._id,
authorName: user.username
});
revalidatePath('/');
return JSON.parse(JSON.stringify(post));
}
How I'm using it:
const handlePostCreate = handleSubmit(data => {
createPost(data).then(() => {
router.push('/');
});
});
6 Replies
try
redirect("/")
inside the server action directly(you won't be able to return a result from server actions though)
Blue-throated HummingbirdOP
Alright, also found the following which may be related to the issue I have
https://github.com/vercel/next.js/pull/53373
https://github.com/vercel/next.js/issues/52075
https://github.com/vercel/next.js/pull/53373
https://github.com/vercel/next.js/issues/52075
Get an error when trying to do that.
weird... then idk, sorry
Blue-throated HummingbirdOP
thank you for the response tho, much appreciated