object is not accepted undefined
I have an object in the convex
parent Document
database that accepts Id<"document"> | undefined
, but when I put undefined nothing changes36 Replies
but I change
undefined
to Id<"documents">
(pass values with this data type) everything works
ChatGPT advised me to do this (specify exactly the object and type), but now it doesn’t work for me in the opposite direction: if the type is Id<"document">
Can you show a bit more of the code? What you have here looks like it should work, although I'd like to see where
rest
comes from. And I'm not sure why you would do const document = await ctx.db.patch(...)
since patch doesn't return anythingupdate func in convex/document.ts
Oh yeah that's not going to work. You need
ctx.db.patch(args.id, {...rest, parentDocument: rest.parentDocument});
to make sure the value is undefined and not missingI did this, and now when I try to assign some value on the other side (with
undefined
on Id<"...">)
it doesn't work
if it helps somehow:
func where undefined (or Id<"...">) to Id<"...">
func where Id<"..."> to undefined
so how to fix thisSorry I don't understand the question
This thread may help https://discord.com/channels/1019350475847499849/1297837121461162006/1297837121461162006
I have the same problem, I tried to do as you wrote, everything is the same. I also tried to separate the functions, one to set
undefined
, the other Id<...>
, in the end nothing**
In short, I don’t want to set the value to undefined
The arguments object can't carry undefined values to the backend, as it needs to be stringified first:
What is your goal? To remove the field or to leave it as-is?
remove
I would recommend sending null instead of undefined. Then in your Convex mutation, if you have null, set it to undefined there.
undefined
is not a value that can be stringified in JSON.I'll try now
If you want to delete the field, you cannot pass the patch argument directly from the client. You would need to explicitly set undefined in the patch argument
Btw this thread prompted me to describe the
undefined
behavior in docs, since i would rather link docs than old discord threads 🙂I've wrestled with this a good bit, and had a stringifiable undefined token in use at one point, but undefined is just a weird value and it's hard to not run into bugs if you start messing with it this way. I dislike null as a js languge feature but I think it's the way to go for this sort of thing.
i did like this by adding an additional null type, but now if i pass values with type
Id<"...">
then it does not change undefined
to Id
I convey it like this
It does not changeWhat do you mean The patch you pasted will setundefined
toId
parentDocument
to undefined
if null is receivedWell, my object takes two types (already three):
Id<"documents"> | undefined | null
after what I added, if I pass null
, then everything is ok and it removes the values in the database and puts undefined
but if I pass Id<"documents">
then it does not change the value in the database
.oh, can you share the code that does that
on the client I mean
and a bit more of the mutation to show where the id is being received and
rest
is being created, etc
client might be enough though
rest in mutation
Just do some basic debugging, I'd start with logging the type of
rest.parentDocument
when you expect an id. If it's undefined, that means you're not sending a value from the client, so you can start logging from there to trace why it's not happening.I did a debug, all data and values from the client are sent correctly
Can you run
ctx.db.get()
again after you patch and log the result
I'm assuming when you say it's not written to the database, you're not relying on the value returned from the mutation (as patch returns undefined, so this function returns undefined)i mean on the backend, inside your mutation
You're just looking to see if the log output there includes your parentDocument id.
So, I did a debug and here you can see that the first time it does everything ok and sets the required value, but the second time it removes it completely
although this was noticeable on the site itself
Do you know where the second call is coming from
perhaps, since I call this function in other files
although I checked and made a separate function for this action, it apparently also ran twice
I also call the component once in layout.tsx
yeah, you'll just need to figure out where exactly you're calling it twice, that isn't a bug in Convex or anything
Ok, I got it, thanks for the help
I'd search for every use of
api.document.update
in your client code and log from each call site to narrow down where they're coming from.sounds like you might want
which would have the behavior where
{parentDocument: null}
removes the parentDocument
field, {}
or {parentDocument: undefined}
leaves it as-is, and {parentDocument: id}
sets it to a string.In general, I figured out the problem. It consisted in the fact that I called the function twice, applying Drag&Drop directly to the parent and child elements simultaneously
all this with nulls, no checks were needed. It was only necessary to apply drag&drop to another container
But thank you anyway, you gave me a push in what direction I need to think
Nice, glad you got it figured out!