Confusing errors on db.patch calls inside mutation

Hi, I'm making some changes to handle a new webhook event and started seeing errors when my mutation is called. The errors complain about missing required fields when calling db.patch:
Uncaught Error: Failed to insert or update a document in table "externalPayment" because it does not match the schema: Object is missing the required field `status`. Consider wrapping the field validator in `v.optional(...)` if this is expected.
Uncaught Error: Failed to insert or update a document in table "externalPayment" because it does not match the schema: Object is missing the required field `status`. Consider wrapping the field validator in `v.optional(...)` if this is expected.
However, I was under the impression that all fields are optional on db.patch (it will update only the received ones), as is hinted by the method signature expecting the "partial" object:
.patch<"externalPayment">(id: Id<"externalPayment">, value: Partial<{
_id: Id<"externalPayment">;
_creationTime: number;
paymentId?: string | undefined;
ticketId: string;
method: string;
amount: number;
tip: number;
status: string;
.patch<"externalPayment">(id: Id<"externalPayment">, value: Partial<{
_id: Id<"externalPayment">;
_creationTime: number;
paymentId?: string | undefined;
ticketId: string;
method: string;
amount: number;
tip: number;
status: string;
and so a "required field" validation against the schema doesn't really make sense. However, this is behaving inconsistently, as shown in the attached screenshots. As it was working properly before today, I suspect that the real culprit might be the multiple mutations running in parallel, since I also got this error once:
Documents read from or written to the "externalPayment" table changed while this mutation was being run and on every subsequent retry. Another call to this mutation changed the document with ID "jx72sxepckwzjjbq6be4fec1en6xm7e9". See https://docs.convex.dev/error#1
Documents read from or written to the "externalPayment" table changed while this mutation was being run and on every subsequent retry. Another call to this mutation changed the document with ID "jx72sxepckwzjjbq6be4fec1en6xm7e9". See https://docs.convex.dev/error#1
So why am I seeing the schema validation errors? Are these two related? I'll work to fix the concurrency issue regardless, but it took me a while to get the concurrency error so if it is the real cause it would have been better to have seen this first. Am I missing anything? I will appreciate any help.
No description
No description
2 Replies
lee
lee6mo ago
patch has an interesting behavior: if a field is missing from the argument, it is left as-is. but if a field exists with value undefined, the field is removed from the document. so if you want a field to be ignored, you should delete it from the patch argument, instead of setting it to undefined i.e. {foo: "abc", status: undefined} is different from {foo: "abc"} when used as an argument to patch
ian
ian6mo ago
One interesting thing I learned about js recently, in order to conditionally add a field:
> { a: 3, b: 1 == 0 ? 'foo' : undefined }
{ a: 3, b: undefined }
> { a: 3, ...(1 == 0 ? {b: 'foo'} : null) }
{ a: 3 }
> { a: 3, ...(1 == 0 ? {b: 'foo'} : {}) }
{ a: 3 }
> { a: 3, b: 1 == 0 ? 'foo' : undefined }
{ a: 3, b: undefined }
> { a: 3, ...(1 == 0 ? {b: 'foo'} : null) }
{ a: 3 }
> { a: 3, ...(1 == 0 ? {b: 'foo'} : {}) }
{ a: 3 }
The second one surprised me - you can spread in null and it will no-op. However you can't spread null into an array:
> {...null}
{}
> [...null]
Uncaught TypeError: null is not iterable
> {...null}
{}
> [...null]
Uncaught TypeError: null is not iterable
So probably best to spread in {} which is also more understandable / self-documenting

Did you find this page helpful?