vors
vors•3y ago

I m confused about the schema `s

I'm confused about the schema s.optional Does it produce undefined, null? Seems like undefined should be acceptable, but why in this case I'm getting errors when I try to write underfined?
Uncaught Error: undefined is not a valid Convex value (present at path .author in original object {"text":"generated images for prompt: \"horses old\", negative prompt: \"new hello\"","project":{"$id":"projects|mYmq-M1gCCcl0ag4LGlLzA"},"author":"undefined","authorName":"undefined"}). To learn about Convex's supported types, see https://docs.convex.dev/using/types.
at convexToJsonInternal (../../node_modules/convex/src/values/values.ts:395:6)
at convexToJsonInternal (../../node_modules/convex/src/values/values.ts:481:2)
at convexToJson (../../node_modules/convex/src/values/values.ts:499:0)
at insert [as insert] (../../node_modules/convex/src/server/impl/database_impl.ts:35:7)
at <anonymous> (../convex/storeImagesInGeneration.ts:31:1)
at async invokeMutation (../../node_modules/convex/src/server/impl/registration_impl.ts:38:14)
Uncaught Error: undefined is not a valid Convex value (present at path .author in original object {"text":"generated images for prompt: \"horses old\", negative prompt: \"new hello\"","project":{"$id":"projects|mYmq-M1gCCcl0ag4LGlLzA"},"author":"undefined","authorName":"undefined"}). To learn about Convex's supported types, see https://docs.convex.dev/using/types.
at convexToJsonInternal (../../node_modules/convex/src/values/values.ts:395:6)
at convexToJsonInternal (../../node_modules/convex/src/values/values.ts:481:2)
at convexToJson (../../node_modules/convex/src/values/values.ts:499:0)
at insert [as insert] (../../node_modules/convex/src/server/impl/database_impl.ts:35:7)
at <anonymous> (../convex/storeImagesInGeneration.ts:31:1)
at async invokeMutation (../../node_modules/convex/src/server/impl/registration_impl.ts:38:14)
9 Replies
vors
vorsOP•3y ago
here is the relavant doc
No description
vors
vorsOP•3y ago
however TypeScript complains if I try to use null.
ari
ari•3y ago
s.optional will work if the property is missing on your document, but not if it is explicitly set to undefined
vors
vorsOP•3y ago
I settled on s.union(s.id("users"), s.null()), but that's mighty confusing 😆
ari
ari•3y ago
Yeah, I can see how the current doc here can be misleading: https://docs.convex.dev/using/schemas#optional-fields. We should update it to be more clear that this works only with missing fields
Defining a Schema | Convex Developer Hub
End-to-end type safety requires typing your tables.
ari
ari•3y ago
The union with null works too if you prefer setting the value explicitly!
Dan Mercer
Dan Mercer•3y ago
Does this mean you have to use delete document.myfield to make a field go from present to missing? And that there's no way to do that using db.patch? Or is there some way I'm not thinking of? It seems like undefined should be treated the same as "not present", like JSON.parse and .stringify do.
vors
vorsOP•3y ago
Ok I see. Yes, seems like an opportunity for better documentation as usual 🙂
ballingt
ballingt•3y ago
that's correct, there's no way to do this from db.patch now, you have to db.replace. This is annoying, but's safe to use
const val = db.get(id);
delete val.field;
db.replace(id, val);
const val = db.get(id);
delete val.field;
db.replace(id, val);
because the whole mutation is a transaction

Did you find this page helpful?