Sudoku Solver : [SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON]

I stumbled across this error when testing my unfinished project (Sudoku Solver / Quality Assurance)

As I understand, because of server error - instead of JSON, HTML is being sent (as response). Error points to fetch() method.

No clue how to solve this :face_with_spiral_eyes:

api.js :

'use strict';

const SudokuSolver = require('../controllers/sudoku-solver.js');

module.exports = function (app) {
  
  let solver = new SudokuSolver();

  app.route('/api/check')
    .post((req, res) => {
      let puzzle = req.body.puzzle
      let coordinate = req.body.coordinate
      let rowCoor = coordinate[0]
      let colCoor = coordinate[1]
      let value = req.body.value

      let conflict = [];

      // check rows
      if (solver.checkRowPlacement(puzzle, rowCoor, colCoor, value)) {
        conflict.push("row")
      }

      // check columnns
      if (solver.checkColPlacement(puzzle, rowCoor, colCoor, value)) {
        conflict.push("column")
      }

      // check region
      if (solver.checkRegionPlacement(puzzle, coordinate, value)){
        conflict.push("region")
      }

      // generate response
      if (conflict.length === 0) {
        res.json({
          valid: true
        })

      } else {
        res.json({
          valid: false,
          conflict: conflict
        })
      }

    });
    
  app.route('/api/solve')
    .post((req, res) => {
      let puzzle = req.body.puzzle
      let output = solver.validate(puzzle)

      output 
        ? res.json({error: output}) 
        : res.json({error: "no error"})
    });
};

I think we need to see all of the code. Post a repo with it.

I assume the 500 is causing an HTML response instead of a JSON response. Not sure what is causing the server to throw.

thx for responding.

It is throwing on line 222. You should be able to figure out why if you debug it. Look at what indexOf is returning before it.

1 Like