Next.js Discord

Discord Forum

Uncaught Error: Only plain objects, and a few built-ins, can be pass

Answered
West African Lion posted this in #help-forum
Open in Discord
Avatar
West African LionOP
I'm trying to call a server function when the user submits a form, but I keep getting the above error
I've attached the related code
What I understood from searching google was that it's related to passing non serialisable objects from server to client, but I couldn't figure out why in my case.
Image
Image
Image
Image
Answered by James4u (Tag me if needed)
const product = db.insert(products).values(validatedData.data);
return {product: JSON.stringify(product)}
View full answer

26 Replies

Avatar
@West African Lion
as it says you can't pass the plain object to the client.
const data = JSON.parse(JSON.stringify(sampleData))

stringify your obj and return to the client end
Avatar
West African LionOP
this still returns the same, although I did try with JSON.stringify() alone .. and it works .. but I get invalid data from the server action
Avatar
const product = db.insert(products).values(validatedData.data);
return {product: JSON.stringify(product)}
Answer
Avatar
it should work
what invalid data do you exactly get?
Avatar
West African LionOP
sorry for replying late,
I get this when I log the validatedData variable
{ success: false, error: [Getter] }
and as a result of strigifying the object, I get that type string isn't assignable to the newProduct var type
Image
I think the issue is related to prototype key being also added, which isn't serialisable?
Image
Avatar
@West African Lion
I am talking about stringifying the result of the added product from the server action
the last screenshot you shared is about the form submit handler, no?
Avatar
West African LionOP
Yes,
Avatar
⬆️
you can pass object from form handler to the server action
but when you return something from sever action to the client end, you should serialize the payload
@West African Lion does it make sense?
Avatar
West African LionOP
it returbs this
Image
but what is it that m returning?
Avatar
you are returning the newly created product from your server action, right?
return db.insert(products).values(validatedData.data);
stringify the payload and return it
yeah, it's stringified product payload
so do you still get the error?
Avatar
West African LionOP
aah I feel so stupid rn, I was so focused on the error being in the product form that I didn't check that the products are actually being created, what made it even more confusing was that I wasn't querying the products I was just passing an mepty array
man thank you .. so the issue wasn't in the product form .. it was in the return value of the insert drizzle command, Got it
somehow I didn't see this, But thank you so much, I appreciate your patience
Avatar
no worries, glad that you realized the issue!