Server Action/Server Component help
Unanswered
Checkered Giant posted this in #help-forum
Checkered GiantOP
trying to explore the feature with a low effort CRUD app and what's happening is when I make my post and revalidate the path, I only see the resulting data in the server component after every 2 requests. the pattern has been:
POST 1 - looks like nothing happened
POST 2 - both post 1 and 2 appear
I'm using sqlite as of now
Here's my action.ts
and here's my server component
POST 1 - looks like nothing happened
POST 2 - both post 1 and 2 appear
I'm using sqlite as of now
Here's my action.ts
`
export async function createUser(formData: z.infer<typeof formSchema>) {
try {
const newDB = await db.run(`INSERT INTO User (username, age) VALUES('${formData.username}', ${formData.age})`)
console.log(newDB)
revalidatePath('/')
return { message: 'User created', success: true };
} catch (e){
return { message: "No user created", success: false }
}
}
}
and here's my server component
import React, { Suspense } from "react";
import { db } from "@/lib/db";
import Loading from "../ui/Loading";
interface User {
username: string;
age: number;
}
const DashboardTable = async () => {
const arr: User[] = await new Promise((resolve, reject) => {
db.all(`SELECT * FROM User`, (err: any, row: User[]) => {
if (err) {
return reject(err);
}
resolve(row);
console.log(row)
});
});
return (
<>
<Suspense fallback={<Loading />}>
{arr.map((user) => (
<div key={user.username}>{user.username}</div>
))}
</Suspense>
</>
);
};
export default DashboardTable;