错误:ER空查询:使用mySQL在node.js中查询为空

kpbwa7wx  于 2023-01-08  发布在  Mysql
关注(0)|答案(1)|浏览(228)

I'm working on a login form with node.js and MySQL as the database. When I run the program and try to register a new user I get this error when I press submit.
When I press submit I get the following error in the console in the inspect page in chrome: error and I get in the terminal inside vs code the following error:
node:internal/process/promises:288 triggerUncaughtException(err, true /* fromPromise */); ^ Error: ER_EMPTY_QUERY: Query was empty at Sequence._packetToError (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14) at Query.ErrorPacket (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\protocol\sequences\Query.js:79:18) at Protocol._parsePacket (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\protocol\Protocol.js:291:23) at Parser._parsePacket (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\protocol\Parser.js:433:10) at Parser.write (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\protocol\Parser.js:43:10) at Protocol.write (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\protocol\Protocol.js:38:16) at Socket. (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\Connection.js:88:28) at Socket. (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\Connection.js:526:10) at Socket.emit (node:events:513:28) at addChunk (node:internal/streams/readable:324:12) -------------------- at Protocol._enqueue (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\protocol\Protocol.js:144:48) at Connection.query (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\mysql\lib\Connection.js:198:25) at register (D:\programmeer stuff\javascript\informatica site\New folder\controllers\register.js:9:12) at Layer.handle [as handle_request] (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\express\lib\router\layer.js:95:5) at next (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\express\lib\router\route.js:144:13) at Route.dispatch (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\express\lib\router\route.js:114:3) at Layer.handle [as handle_request] (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\express\lib\router\layer.js:95:5) at D:\programmeer stuff\javascript\informatica site\New folder\node_modules\express\lib\router\index.js:284:15 at Function.process_params (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\express\lib\router\index.js:346:12) at next (D:\programmeer stuff\javascript\informatica site\New folder\node_modules\express\lib\router\index.js:280:10) { code: 'ER_EMPTY_QUERY', errno: 1065, sqlMessage: 'Query was empty', sqlState: '42000', index: 0, sql: undefined }
The code I try to run is:
controllers/register.js

const db = require("../routes/db-config");
const bcrypt =require("bcryptjs")

const register = async (req, res) => {
    const {email, password: Npassword} = req.body
    if(!email || !Npassword) return res.json({satus:'error', error:"Please enter your email and password"});
    else{
        console.log(email);
        db.query('SELECT email FROM users WHERE email = ?' [email], async(err, result) => {
            if(err) throw err;
            if(result[0])return res.json({satus:'error', error:"email already in use"})
            else {
                const password = bcrypt.hash(Npassword, 8);
                console.log(password)
                db.query('INSERT INTO users SET ?', {email: email, password: password}, (error, results)=>{
                    if (error) throw error;
                    return res.json({ satus:"success", success:"account registred succesfully" })
                })
            }
        })
    }
}
module.exports = register;

I think either db.query('INSERT INTO users SET ?', {email: email, password: password}, (error, results)=> or db.query('SELECT email FROM users WHERE email = ?' [email], async(err, result) => is cousing the error
js/register.js here is according to the inspect page in chrome the error

form.addEventListener("submit", () => {
    const register = {
        email: email.value,
        password: password.value
    }
    fetch("/api/register", {
        method: "POST",
        body: JSON.stringify(register),
        headers: {
            "Content-Type":"application/json"
        }
    }).then(res => res.json())
        .then(data  => {
            if(data.status == "errpr") {
                success.style.display = "none"
                error.style.display = "block"
                error.innerText = data.error
            } else {
                error.style.display = "none"
                success.style.display = "block"
                success.innerText = data.success 
            }
        })
})

routes/db-config

const sql = require("mysql");
const dotenv = require("dotenv").config();
const db = sql.createConnection({
    host:process.env.DATABASE_HOST,
    user:process.env.DATABASE_USER,
    password:process.env.DATABASE_PASSWORD,
    database:process.env.DATABASE
})

module.exports = db;

If more pieces of code are needed to help me solve this problem please let me know!!

mklgxw1f

mklgxw1f1#

在这一行

db.query('SELECT email FROM users WHERE email = ?' [email], async(err, result) => {

您应该在查询字符串和它应该位于的列表之间放置逗号","

db.query('SELECT email FROM users WHERE email = ?', [email], async(err, result) => {

相关问题