Graphql server stops working after throwing validation error

My app works fine until I got the validation error, does anyone know why? AM I using the validation in the wrong way? Thanks!

I see this on the server end:
Error: Phoebe Hu has enrolled for MathCount Prep
api | at null. (/Users/jd/MyProjects/p1/api/src/services/orders/orders.ts:49:15)
web | [webpack-dev-server] [HPM] Error occurred while proxying request localhost:8910/graphql to http://localhost:8911/ [ECONNRESET] (Errors | Node.js v19.1.0 Documentation)

and this on client end:
Error: The RedwoodJS API server is not available or is currently reloading. Please refresh

I have this validator and it only happens

  validateWith(async () => {
    let duplicated = false
    console.log(enrollments)
    enrollments.forEach(async (item) => {
      const result = await db.studentOnRecurrence.findFirst({
        select: {
          id: true,
        },
        where: {
          studentId: item.studentId,
          recurrenceId: item.recurrenceId,
        },
      })
      console.log(result)
      if (result) {
        duplicated = true
      }
      if (duplicated) {
        throw new Error(
          `${item.studentName} has enrolled for ${item.lessonName}`
        )
      }
    })
  })

I have made it work after poking around, here is what I did:

  1. move the duplication checking logic out side validateWith
  2. use for loop instead of forEach to make sure the checking code runs sync.
  3. remove async on the anonymous function in validateWith

  let badItem
  for (let i = 0; i < enrollments.length; i++) {
    const item = enrollments[I]
    const result = await db.studentOnRecurrence.findFirst({
      select: {
        id: true,
      },
      where: {
        studentId: item.studentId,
        recurrenceId: item.recurrenceId,
      },
    })
    console.log(result)
    if (result) {
      badItem = item
      break
    }
  }

validateWith(() => {
      if (badItem) {
        throw new Error(
          `${item.studentName} has enrolled for ${item.lessonName}`
        )
      }
  })

this does work, BUT but I still have some question to be answered:

  1. why does it break the graphQL server if async is used for the validation fix?
  2. the solution does feel right, how can I warp the checking logic into the invalidateWith function?
  3. If there is no validation, it will throw exception because of unique key violation as expected, but
    afterward, The Who graphQL server is down, no api call works, here is the error

enrollments.forEach(async (item) => {
api | → 91 await db.studentOnRecurrence.create(
api | Unique constraint failed on the fields: (studentId,recurrenceId)
api | at RequestHandler.handleRequestError (/Users/jd/MyProjects/p1/node_modules/@prisma/client/runtime/index.js:29909:13)
api | at RequestHandler.request (/Users/jd/MyProjects/p1/node_modules/@prisma/client/runtime/index.js:29892:12)
api | at async PrismaClient._request (/Users/did/MyProjects/p1/node_modules/@prisma/client/runtime/index.js:30864:16)
api | at null. (/Users/jd/MyProjects/p1/api/src/services/orders/orders.ts:91:5) {
api | code: ‘P2002’,
api | clientVersion: ‘4.3.1’,
api | meta: { target: [ ‘studentId’, ‘recurrenceId’ ] }
api | }
web | [webpack-dev-server] [HPM] Error occurred while proxying request localhost:8910/graphql to http://localhost:8911/ [ECONNRESET] (Errors | Node.js v19.1.0 Documentation)

Thanks