I am trying to make a crud app but I am getting an error message "TypeError: console.log is not a function" at Query. (C:UsersLuis HernandezDesktopgaming-crudserverapp.js:30:25)
[![enter image description here][1]][1]This happens every time I click the add button to add a game to the database.
This is the server
//create server
const express = require('express')
const app = express();
const mysql = require('mysql')
const cors = require('cors')
//Make available to make request from the front end to the backend
app.use(cors());
app.use(express.json());
const db = mysql.createConnection({
user: "root",
host: "localhost",
password: "adminsql",
database: "storagedgames"
})
app.post('/create', (req, res) => {
const gameName = req.body.gameName
const genre = req.body.genre
const year = req.body.year
const console = req.body.console
db.query(
'INSERT INTO games (game_name, genre, year, console) VALUES (?,?,?,?)',
[gameName, genre, year, console],
(err, result) => {
if(err){
console.log(err);
} else {
res.send('values added')
}
});
});
// port to listen
app.listen(3001, () => {
console.log('listening to port 3001')
})```
This is the Client frontend
```import { Link } from 'react-router-dom'
import './Create.css'
import { useState } from 'react';
import Axios from 'axios'
const Create = () => {
const [gameName, setGameName] = useState('');
const [genre, setGenre] = useState('');
const [year, setYear] = useState(0);
const [console, setConsole] = useState('');
const addGame = () => {
Axios.post('http://localhost:3001/create',
{
gameName: gameName,
genre: genre,
year: year,
console: console
}).then((result) => {
console.log('added succesfully')
}).catch((err) => {
console.log('error mesagge' + err)
});
}
return (
<div className="create">
<Link to={'/'} className='home-button neon-button-form'>Home</Link>
<div className="create-container ">
<h2 className="create-title headtext-h2">Add a Game</h2>
<form className="create-form">
<label className='create-label headtext-label'>Game Name</label>
<input type="text" className='create-input ' onChange={(e) => {
setGameName(e.target.value);
}} />
<label className='create-label headtext-label'>Genre</label>
<input type="text" className='create-input' onChange={(e) => {
setGenre(e.target.value);
}} />
<label className='create-label headtext-label'>Year</label>
<input type="number" className='create-input' onChange={(e) => {
setYear(e.target.value);
}} />
<label className='create-label headtext-label'>Console</label>
<input type="text" className='create-input' onChange={(e) => {
setConsole(e.target.value);
}} />
<button className='create-button neon-button-form' onClick={()=>{addGame()}}>ADD</button>
</form>
</div>
</div>
);
}
export default Create;```
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/UHUyP.png
[2]: https://i.stack.imgur.com/EkngN.png
2
Answers
The problem in this line
You assigned a different value to console, which would cause the error. When you assigned
console
like this, your console object won’t havelog()
function. The wordconsole
is a reserved keyword in JavaScript, which refers to the console object used for logging messages. You can rename it to bodyConsole or something similarI saw similar
[console, setConsole]
in your front-end code. You should rename it to something else as wellYou’ve already got an answer before me.
You declared a varibale named "console" which makes you confused for that between a name of "console" you declared and the console object which provides access to the browser’s debugging console.
therefore you must change the name "console" into something else like "bodyConsole", "consoleValue", etc …