#TransactionID undefined after calling payload.findByID

3 messages · Page 1 of 1 (latest)

young matrix
#

I have a problem with transactions in payload local API. After creating a transaction and adding it to the request, it is lost after the first call with the local API.

// within a custom express request handler
const transactionID = await req.payload.db.beginTransaction?.();
console.log(transactionID) // --> '8796cb59-39b3-480c-bda5-248e7039eedc'
req.transactionID = transactionID

await req.payload.findByID({
  ...stuff
  req,
})

console.log(req.transactionID) // --> undefined 🤔

Is this expected behavior?

vagrant sirenBOT
young matrix
#

My code above was too simplified. I was trying to get a record and catching the error by providing fallback data:

  const record = await payload
    .findByID({
      collection: "my_collection",
      id: body.id,
      req: context.req,
      depth: 0,
    })
    .catch(() => ({
      id: body.id,
      user: context.user.id,
      someField: "blabla",
    }));


Payload seems to abort and rollback transactions, when it encounters an error in the local API. So when the record is not found, payload will abort the transaction and the next operations won't be part of this transaction.