Discussion on fixing a transaction mutation error related to incrementing a field that was not present in Sanity.io
25 replies
Last updated: Jun 4, 2020
G
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
G
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
G
does it need a default value if i am incrementing it?
Jun 4, 2020, 8:38 PM
G
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
L
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
L
Nothing has changed here (at least not deliberately!), did your code change?
Jun 4, 2020, 8:48 PM
L
Unrelated: Should you not be calling
setIfMissingwith
0?
Jun 4, 2020, 8:48 PM
G
yes! sorry was just testing
Jun 4, 2020, 8:49 PM
G
i realize i must have added the inc after my initial tests
Jun 4, 2020, 8:49 PM
G
so did not notice the fail for new transaction
Jun 4, 2020, 8:50 PM
L
Is it still failing with
setIfMissing+
inc?
Jun 4, 2020, 8:52 PM
G
nope!
Jun 4, 2020, 8:53 PM
G
all good now
Jun 4, 2020, 8:54 PM
L
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
L
Not sure what you're using to build your patch — the above is with the official JS client.
Jun 4, 2020, 8:56 PM
L
Jun 4, 2020, 8:56 PM
L
Oh yeah, it has a "chained mutations" thing, I haven't used that.
Jun 4, 2020, 8:56 PM
L
Same principle, though!
Jun 4, 2020, 8:57 PM
L
await client .patch(customer.id.toString(), patch => patch .setIfMissing({tickets: 0}) .inc({tickets: customerTickets})) .commit()
Jun 4, 2020, 8:57 PM
G
yep using that pattern
Jun 4, 2020, 8:59 PM
G
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
G
i'm sure i can clean that up but it works
Jun 4, 2020, 9:02 PM
L
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
G
ah true they were all chained before
Jun 4, 2020, 9:04 PM
G
cleaning it up
Jun 4, 2020, 9:05 PM
L
👍
Jun 4, 2020, 9:05 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.