error: invalid input syntax for type json when submitting converted circular structure

I haven’t encountered anything like this before. I have a NextJS 13 project, using PostgreSQL.

Initially when submitting the data through the API route, I was getting the “TypeError: Converting circular structure to JSON.”

So I found a function to convert the circular function, which seemed to work.

const getCircularReplacer = () => {
      const seen = new WeakSet();
      return (key, value) => {
        if (typeof value === 'object' && value !== null) {
          if (seen.has(value)) {
            return;
          }
          seen.add(value);
        }
        return value;
      };
    };

This is where I get the invalid input syntax for type json.

Here is my patch function:

export async function PATCH(request: Request) {
  const body = await request.json()
  const {databaseId, boardStatus, updatedTasks} = body;

  try {
    const query = 'Update boards SET status = $1, tasks = $2 WHERE id = $3';
    const values = [boardStatus, updatedTasks, databaseId];
    const result = await conn.query(query, values);

    return NextResponse.json(result);
  } catch (error) {
    console.log(error)
    throw new Error('Failed to update task')
  }
}

So when I console log the individual values I get the value first but then secondary it logs null.

So “boardStatus” first logs the string “Now”, which is correct but then right after undefined.

“updatedTasks” logs and array with two objects (the tasks) but then immediately [null, null]

“databaseId” is a strange one. It logs the number 7 but then logs the following:

{
  _reactName: 'onSubmit',
  _targetInst: null,
  type: 'submit',
  nativeEvent: { isTrusted: true },
  target: {
    '0': {
      '__reactFiber$j2ruhtxtfqr': [Object],
      '__reactEvents$j2ruhtxtfqr': {},
      value: 'Launch version one',
      _valueTracker: {}
    }
  },
  eventPhase: 3,
  bubbles: true,
  cancelable: true,
  timeStamp: 152898.19999992847,
  defaultPrevented: false,
  isTrusted: true
}

I get two errors. Error one seems to point to a problem with the structure of the data but it looks ok to me.

length: 242,
  severity: 'ERROR',
  code: '22P02',
  detail: 'Expected ":", but found ",".',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: 'JSON data, line 1: ...se}],\"description\":\"Test save to database\"}",...n' +
    "unnamed portal parameter $2 = '...'",
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'jsonfuncs.c',
  line: '646',
  routine: 'json_errsave_error'

Error two is trying to process the secondary [null, null]. I don’t know where this is coming from, unless converting the circular structure did this.

length: 191,
  severity: 'ERROR',
  code: '22P02',
  detail: 'Token "NULL" is invalid.',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: "JSON data, line 1: {NULL...nunnamed portal parameter $2 = '...'",
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'jsonfuncs.c',
  line: '646',
  routine: 'json_errsave_error'
}

Here is the console.log of the tasks, which is where it seems to hang up:

[
  {
    title: 'Launch version one',
    status: '',
    subtasks: [ [Object], [Object] ],
    description: 'Test save to database'
  },
  {
    title: 'Review early feedback and plan next steps for roadmap',
    status: '',
    subtasks: [ [Object], [Object], [Object] ],
    description: "Beyond the initial launch, we're keeping the initial roadmap completely empty. This meeting will help us plan out our next steps based on actual customer feedback."
  }
]
[ null, null ]

Thanks much for any direction!