You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a fairly basic demo i'm trying to put together that uses D1 and Drizzle. I'm not quite sure instantiating my d1 database correctly. Here is what I'm trying to do:
import{drizzle}from"drizzle-orm/d1";import{revalidatePath}from"next/cache";import{getRequestContext}from"@cloudflare/next-on-pages";import{todo}from"@/schemas";exportconstruntime="edge";exportdefaultasyncfunctionHome(){constAPP_DB=getRequestContext().env.APP_DB;constdb=drizzle(APP_DB);consttodos=awaitdb.select().from(todo);asyncfunctionaddTodo(formData: FormData){"use server";try{constformTodo=formData.get("todo")asstring;awaitdb.insert(todo).values({text: formTodo,timestamp: Date.now()});}catch(error){console.warn("Unhandled error...",error);}}return(<main><formaction={addTodo}><inputname="todo"type="text"/><buttonclassName="btn"type="submit">
Add Todo
</button></form>{todos.map((todo)=>(<divkey={todo.id}>{todo.text}</div>))}</main>);}
When creating a todo I keep getting this error when submitting a new todo:
Error: Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server". Or maybe you meant to call this function rather than return it.
[function insert]
^^^^^^^^^^^^^^^
I can make this error go away by moving these lines outside of the component definition
import{drizzle}from"drizzle-orm/d1";import{revalidatePath}from"next/cache";import{getRequestContext}from"@cloudflare/next-on-pages";import{todo}from"@/schemas";exportconstruntime="edge";constAPP_DB=getRequestContext().env.APP_DB;constdb=drizzle(APP_DB);exportdefaultasyncfunctionHome(){consttodos=awaitdb.select().from(todo);asyncfunctionaddTodo(formData: FormData){"use server";try{constformTodo=formData.get("todo")asstring;awaitdb.insert(todo).values({text: formTodo,timestamp: Date.now()});}catch(error){console.warn("Unhandled error...",error);}}return(<main><formaction={addTodo}><inputname="todo"type="text"/><buttonclassName="btn"type="submit">
Add Todo
</button></form>{todos.map((todo)=>(<divkey={todo.id}>{todo.text}</div>))}</main>);}
This works and my app works locally, however my project no longer builds... And I get this error in my CI:
15:59:57.956 | ▲ Error:
-- | --
15:59:57.956 | ▲ `getRequestContext` is being called at the top level of a route file, this is not supported
15:59:57.957 | ▲ for more details see https://developers.cloudflare.com/pages/framework-guides/nextjs/deploy-a-nextjs-site/#top-level-getrequestcontext
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a fairly basic demo i'm trying to put together that uses D1 and Drizzle. I'm not quite sure instantiating my d1 database correctly. Here is what I'm trying to do:
When creating a todo I keep getting this error when submitting a new
todo
:I can make this error go away by moving these lines outside of the component definition
like so:
This works and my app works locally, however my project no longer builds... And I get this error in my CI:
Very confused what I'm doing wrong...
fully reproducible repo : https://github.com/chiubaca/fullstack-next-cloudflare
demo application here using first example : https://fullstack-next-cloudflare.pages.dev/
Beta Was this translation helpful? Give feedback.
All reactions