Unable to insert into db with a server action
Unanswered
piscopancer posted this in #help-forum
I use drizzle orm, turso driver, local.db file and next v14.
my async component is not marked with
my async component is not marked with
use client. It is a separate file. await db.insert is what causes the error, the app does not crash when this function is commented outimport { getUser } from '@/user'
import { db } from '@/db'
import { likesToQuestionsTable } from '@/db/schema'
export default async function Question({ props: ...}) {
const user = await getUser()
async function tryLeaveLike(formData: FormData) {
'use server'
const userId = formData.get('user-id')?.toString()
if (!userId) return
console.log(userId)
âš âš âš
// await db.insert(likesToQuestionsTable).values({
// userId,
// questionId: question.id.toString(),
// })
âš âš âš
}
return (
...
<form action={tryLeaveLike}>
<input hidden name='user-id' value={user?.db.id} />
<button type='submit'>
...
</button>
</form>
...
)11 Replies
⨯ Error: Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server".
[function]for some reason it thinks that my component is a client component...
@Ray no problem, when I can use my computer, I'll send the code. Also I managed to make it work by pure intuition as I moved the server function to the actions.ts file and imported it into the component instead of declaring it in the scope of the component. It is not mentioned on the docs in any form though
@piscopancer <@743561772069421169> no problem, when I can use my computer, I'll send the code. Also I managed to make it work by pure intuition as I moved the server function to the actions.ts file and imported it into the component instead of declaring it in the scope of the component. It is not mentioned on the docs in any form though
oh yeah its better to do that instead if the action contain some external packages
so I simply moved the function away into the module with a "use server" declaration or prefix whatever name it is supposed to have
yeah, strange.... but it works.. strange 😕
I had alot of issue with turso and server action before
but it got fixed
what was fixed?
unable to import the server action to client component