✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

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
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
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
does it need a default value if i am incrementing it?
Jun 4, 2020, 8:38 PM
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
It needs to be already set —
inc
will fail if the field doesn't exist or if it's not a number
Jun 4, 2020, 8:47 PM
Nothing has changed here (at least not deliberately!), did your code change?
Jun 4, 2020, 8:48 PM
Unrelated: Should you not be calling
setIfMissing
with
0
?
Jun 4, 2020, 8:48 PM
yes! sorry was just testing
Jun 4, 2020, 8:49 PM
i realize i must have added the inc after my initial tests
Jun 4, 2020, 8:49 PM
so did not notice the fail for new transaction
Jun 4, 2020, 8:50 PM
Is it still failing with
setIfMissing
+
inc
?
Jun 4, 2020, 8:52 PM
nope!
Jun 4, 2020, 8:53 PM
all good now
Jun 4, 2020, 8:54 PM
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
Not sure what you're using to build your patch — the above is with the official JS client.
Jun 4, 2020, 8:56 PM
Oh yeah, it has a "chained mutations" thing, I haven't used that.
Jun 4, 2020, 8:56 PM
Same principle, though!
Jun 4, 2020, 8:57 PM
await client
  .patch(customer.id.toString(), patch =>
    patch
      .setIfMissing({tickets: 0})
      .inc({tickets: customerTickets}))
  .commit()
Jun 4, 2020, 8:57 PM
yep using that pattern
Jun 4, 2020, 8:59 PM
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
i'm sure i can clean that up but it works
Jun 4, 2020, 9:02 PM
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
ah true they were all chained before
Jun 4, 2020, 9:04 PM
cleaning it up
Jun 4, 2020, 9:05 PM
👍
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.

Was this answer helpful?