I’m using an API that solves the Sudoku game. If I understand it right my issue is fetching the data from the API. The API is returning data. I have also tried without the backend and received the 400 bad request error. I’m quite new to programming and would need your help, please.
Here is the link to the error message https://imgur.com/a/brFk7Oo
Here is the link to the network error https://imgur.com/PsBsefW
app.js
// Selectors
const puzzleBoard = document.querySelector('#puzzle');
const solveButton = document.querySelector('#solve-button');
const solutionDisplay = document.querySelector('#solution');
const squares = 81;
let submission = [];
//functions
for (let i = 0; i < squares; i++) {
const inputElement = document.createElement('input');
inputElement.setAttribute('type', 'number');
inputElement.setAttribute('min', '1');
inputElement.setAttribute('max', '9');
if (
((i % 9 == 0 || i % 9 == 1 || i % 9 == 2) && i < 21) ||
((i % 9 == 6 || i % 9 == 7 || i % 9 == 8) && i < 27) ||
((i % 9 == 3 || i % 9 == 4 || i % 9 == 5) && (i > 27 && i < 53)) ||
((i % 9 == 0 || i % 9 == 1 || i % 9 == 2) && i > 53) ||
((i % 9 == 6 || i % 9 == 7 || i % 9 == 8) && i > 53)
) {
inputElement.classList.add('odd-section')
}
puzzleBoard.appendChild(inputElement);
}
// wrapped the document in a new div
const wrapper = document.createElement('div')
puzzleBoard.parentNode.insertBefore(wrapper, puzzleBoard)
wrapper.appendChild(puzzleBoard)
wrapper.className = "puzzle"
const joinValues = () => {
const inputs = document.querySelectorAll('input');
inputs.forEach(input => {
if (input.value) {
submission.push(input.value);
} else {
submission.push('.');
}
})
console.log(submission)
}
const populateValues = (isSolvable, solution) => {
const inputs = document.querySelectorAll('input');
if (isSolvable && solution) {
inputs.forEach((input, i) => {
input.value = solution[i];
})
solutionDisplay.innerHTML = 'Here is the answer!';
} else {
solutionDisplay.innerHTML = 'You made a mistake, this is not solvable';
}
}
const solve = () => {
joinValues()
const data = {numbers: submission.join('')}
console.log('data', data)
fetch('http://localhost:8000/solve', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(data)
}) .then(response => response.json())
.then(data => {
console.log(data)
populateValues(data.solvable, data.solution)
submission = []
})
.catch((error) => {
console.error('Error:', error)
})
}
solveButton.addEventListener('click', solve);
server.js
const PORT = 8000
const axios = require('axios').default
const express = require('express')
const cors = require('cors')
require('dotenv').config()
const app = express()
app.use(cors())
app.use(express.json())
app.post('/solve', (req, res) => {
const options = {
method: 'POST',
url: 'https://solve-sudoku.p.rapidapi.com/',
headers: {
'content-type': 'application/json',
'x-rapidapi-host': 'solve-sudoku.p.rapidapi.com',
'x-rapidapi-key': process.env.RAPID_API_KEY
},
data: {
puzzle: req.body.numbers
}
}
axios.request(options).then((response) => {
console.log(response.data)
res.json(response.data)
}).catch((error) => {
console.error(error)
})
})
app.listen(PORT, () => console.log(`server listening on PORT ${PORT}`))
Would really appreciate any help