I keep getting Error: ER_EMPTY_QUERY: QUERY was empty

49
July 21, 2021, at 02:00 AM

I'm trying to make a register page. When I enter the data that needs to be registered, it shows up on the console followed by Error: ER_EMPTY_QUERY.

I believe the problem is in the controllers/auth.js file but I can't figure it out for some reason. Please help, been stuck on this for the past 2 days.

controllers/auth.js:

const mysql = require('mysql');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs'); 
const db = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'practice'
})
exports.register = (req, res) => {
    console.log(req.body)
    const {name, email, password, passwordConfirm} = req.body
    db.query('SELECT email FROM users WHERE email = ?' [email], async (error, results) => {
        if(error) {
            console.log(error)
        }
        if( results.length > 0 ) {
            return res.render('register', {
                message: 'That email is already is use'
            })
        } else if( password !== passwordConfirm) {
          return res.render('register', {
              message: 'Passwords do not match'
          })
        }
        let hashedPassword = await bcrypt.hash(password, 8);
        console.log(hashedPassword);
        db.query('INSERT INTO users SET ?', {name: name, email: email, password: hashedPassword}, (error, results) => {
            if(error) {
              console.log(error)
            } else {
                console.log(results);
                return res.render('register', {
                    message: 'User registered'
                })
            }
        })

    }) 
  
}

register.hbs:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/styles.css">
    <title>Document</title>
</head>
<body>
    <nav>
        <h4>Node MySQL</h4>
        <ul>
            <li><a href="/">Home</a></li>
            <li><a href="/login">Login</a></li>
            <li><a href="/register">register</a></li>
        </ul>
    </nav>
    <div class="container mt-4">
        <div class="card">
            <div class="card-header">
                Register Form
            </div>
            <div class="card-body">
                <form action="/auth/register" method="POST">
                    <div class="mb-3">
                        <label for="name">Name</label>
                        <input type="text" class="form-control" id="name" name="name">
                    </div>
                    <div class="mb-3">
                        <label for="email">Email address</label>
                        <input type="email" class="form-control" id="email" name="email">
                    </div>
                    <div class="mb-3">
                        <label for="password">Password</label>
                        <input type="password" class="form-control" id="password" name="password">
                    </div>
                    <div class="mb-3">
                        <label for="passwordConfirm">Confirm Password</label>
                        <input type="password" class="form-control" id="passwordConfirm" name="passwordConfirm">
                    </div>
                    <button type="submit" class="btn btn-primary">Submit</button>
                    </form>
            </div>
        </div>
        {{#if message}}
        <h4 class="alert alert-daner mt-4">{{message}}</h4>
        {{/if}}

    </div>

</body>
</html>
READ ALSO
Javascript Operators - Combining Nullish Coalescing with Ternary

Javascript Operators - Combining Nullish Coalescing with Ternary

So I have some code where I'm essentially trying to do an if, else if, else statement in one line, but based on nullishness instead of truthiness

58
Django - JQuery autocomplete custom select from multiple fields

Django - JQuery autocomplete custom select from multiple fields

I have a user search that autocompletes by both ticker and nameThe search results come back as "{{ticker}} - {{name}}"

81
How good implement method? [closed]

How good implement method? [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

56