Uncaught Error: Only plain objects, and a few built-ins, can be pass
Answered
West African Lion posted this in #help-forum
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.
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.
Answered by James4u (Tag me if needed)
const product = db.insert(products).values(validatedData.data);
return {product: JSON.stringify(product)}
26 Replies
@West African Lion
as it says you can't pass the plain object to the client.
stringify your obj and return to the client end
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
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
const product = db.insert(products).values(validatedData.data);
return {product: JSON.stringify(product)}
Answer
it should work
what invalid data do you exactly get?
West African LionOP
sorry for replying late,
I get this when I log the validatedData variable
{ success: false, error: [Getter] }
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
I think the issue is related to prototype key being also added, which isn't serialisable?
@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?
West African LionOP
Yes,
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?
West African LionOP
it returbs this
but what is it that m returning?
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?
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
no worries, glad that you realized the issue!