Discussion on fixing a transaction mutation error related to incrementing a field that was not set in Sanity.io.
25 replies
Last updated: Jun 4, 2020
K
I have a transaction mutation that was working up until a few days ago and not sure if something changed in the sanity client or if I should be building my chained transactions differently? but getting a
Sanity error: s: The mutation(s) failed: Cannot increment "content.main.tickets" because it is not present
Jun 4, 2020, 8:38 PM
K
and on the object i am passing the patch i do have an empty field at content.main.tickets
Jun 4, 2020, 8:38 PM
K
does it need a default value if i am incrementing it?
Jun 4, 2020, 8:38 PM
K
seems i have to set before i inc,
.patch(customer.id.toString(), patch => patch.setIfMissing(customerTickets)) .patch(customer.id.toString(), patch => patch.inc(customerTickets))
Jun 4, 2020, 8:45 PM
A
It needs to be already set β
incwill fail if the field doesn't exist or if it's not a number
Jun 4, 2020, 8:47 PM
A
Nothing has changed here (at least not deliberately!), did your code change?
Jun 4, 2020, 8:48 PM
A
Unrelated: Should you not be calling
setIfMissingwith
0?
Jun 4, 2020, 8:48 PM
K
yes! sorry was just testing
Jun 4, 2020, 8:49 PM
K
i realize i must have added the inc after my initial tests
Jun 4, 2020, 8:49 PM
K
so did not notice the fail for new transaction
Jun 4, 2020, 8:50 PM
A
Is it still failing with
setIfMissing+
inc?
Jun 4, 2020, 8:52 PM
K
nope!
Jun 4, 2020, 8:53 PM
K
all good now
Jun 4, 2020, 8:54 PM
A
You can also multiple patch types in a single patch. Something like:
await client .patch(customer.id.toString()) .setIfMissing({tickets: 0}) .inc({tickets: customerTickets}) .commit()
Jun 4, 2020, 8:54 PM
A
Not sure what you're using to build your patch β the above is with the official JS client.
Jun 4, 2020, 8:56 PM
A
Jun 4, 2020, 8:56 PM
A
Oh yeah, it has a "chained mutations" thing, I haven't used that.
Jun 4, 2020, 8:56 PM
A
Same principle, though!
Jun 4, 2020, 8:57 PM
A
await client .patch(customer.id.toString(), patch => patch .setIfMissing({tickets: 0}) .inc({tickets: customerTickets})) .commit()
Jun 4, 2020, 8:57 PM
K
yep using that pattern
Jun 4, 2020, 8:59 PM
K
got this right now working:
return client .transaction() .createIfNotExists(customerData) .patch(customer.id.toString(), patch => patch.set(customerObject)) .patch(customer.id.toString(), patch => patch .setIfMissing({"content.main.totalSpent": 0}) .inc(customerSpent) .setIfMissing({"content.main.tickets": 0}) .inc(customerTickets)) .commit()
Jun 4, 2020, 9:02 PM
K
i'm sure i can clean that up but it works
Jun 4, 2020, 9:02 PM
A
Yep. You can also move the
patch.set(customerObject)into the second one. Doesn't matter (results in same internal operations), just looks cleaner.
Jun 4, 2020, 9:03 PM
K
ah true they were all chained before
Jun 4, 2020, 9:04 PM
K
cleaning it up
Jun 4, 2020, 9:05 PM
A
π
Jun 4, 2020, 9:05 PM
Sanityβ build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.