Using revalidatePath in server actions
Unanswered
Blue-throated Hummingbird posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
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
![Avatar](https://cdn.discordapp.com/avatars/484037068239142956/f386ac21405ce3442e0f3598223e1cb3.webp?size=256)
joulev
try
redirect("/")
inside the server action directly(you won't be able to return a result from server actions though)
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
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.
![Image](https://cdn.discordapp.com/attachments/1136413043467755590/1136551166801608704/image.png)
![Image](https://cdn.discordapp.com/attachments/1136413043467755590/1136551167116202065/image.png)
![Avatar](https://cdn.discordapp.com/avatars/484037068239142956/f386ac21405ce3442e0f3598223e1cb3.webp?size=256)
joulev
weird... then idk, sorry
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Blue-throated HummingbirdOP
thank you for the response tho, much appreciated