node.js - 错误: ER_PARSE_ERROR: You have an error in your SQL syntax

标签 node.js

我正在使用带有 mysql 的 Express Node.js 来构建 api,并希望将这些 api 连接到前端,但我遇到了一个错误,因此我的应用程序无法正常运行,请告诉我问题是什么我的代码。

我的错误是: 错误:ER_PARSE_ERROR:您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行“USEcoffee_shop”附近使用的正确语法

var LocalStrategy = require("passport-local").Strategy;
var mysql = require('mysql');
var bcrypt = require('bcrypt-nodejs');
var dbconfig = require('./database');

var connection = mysql.createConnection(dbconfig.connection);

connection.query('USE' + dbconfig.database);

module.exports = (passport)=>{
    passport.serializeUser((user,done)=>{
        done(null, user.id);
    });

    passport.deserializeUser((id,done)=>{
        connection.query("SELECT * FROM users WHERE id = ? ", [id],
            (err,rows)=>{
            done(err,rows[0])
        });
    });

passport.use(
    'local-signup',
    new LocalStrategy({
        api_keyField : 'api_key',
        nameField : 'name',
        phoneField : 'phone',
        emailField : 'email',
        photoField : 'photo',
        passwordField : 'password',
        passReqToCallback:true
    },
    (req,email,password,done)=>{
        connection.query("SELECT * FROM users WHERE email = ?",[email],
            (err,rows)=>{
                if(err)
                    return done(err);
                if(rows.lenght){
                    return done(null, false, req.flash('signupMessage','That is Already Taken'));
                }else{
                    var newUserMysql = {
                        api_key : api_key,
                        name : name,
                        phone : phone,
                        email : email,
                        photo : photo,
                        password : bcrypt.hashSync(password, null, null)
                    };

                    var insertQuery = "INSERT INTO users (api_key,name,phone,email,photo,password) VALUES (?,?,?,?,?,?)";
                    connection.query(insertQuery, [newUserMysql.api_key, newUserMysql.name, newUserMysql.phone, newUserMysql.email, newUserMysql.photo, newUserMysql.password],
                        (err,rows)=>{
                            newUserMysql.id = rows.insertId;

                            return done(null, newUserMysql);
                        });

                }

            });
    })

    );

passport.use(
    'local-login',
    new LocalStrategy({
        emailField : 'email',
        passwordField : 'password',
        passReqToCallback:true
    },
    (req,email,password,done)=>{
        connection.query("SELECT * FROM users WHERE email = ?", [email],
            (err,rows)=>{
                if(err)
                    return done(err);
                if (!rows.lenght){
                    return done(null, false, req.flash('loginMessage', 'No User Found'));
                }
                if (!bcrypt.compareSync(password, rows[0].password))
                    return done(null, false, req.flash('loginMessage','Wrong password'));

                    return done(null, rows[0]);
            });
    })

    );
};

最佳答案

这个错误是不言自明的,伙计。

ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'USEcoffee_shop' at line 1

此代码:connection.query('USE' + dbconfig.database); 转换为 connection.query('USEcoffee_shop');

但是 SQL 无法将 USEcoffee_shop 识别为有效语法。 USE 和数据库名称之间必须有一个空格。

因此将您的代码修改为:

connection.query('USE ' + dbconfig.database); //observe the space after USE

这应该有效。

关于node.js - 错误: ER_PARSE_ERROR: You have an error in your SQL syntax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55566977/

相关文章:

node.js - 在 Docker 容器中安装 mongo 客户端

mysql - 使用 Node js 连接 Mysql 抛出错误 "connect ECONNREFUSED 127.0.0.1:3306"

angularjs - Angular 不发送对象中的对象

javascript - 迁移到 meteor / react

javascript - 如何通过代码拼写检查将所有未知单词添加到字典中,或者当语言不是英语时忽略它们?

node.js - Solaris 11 : Node. JS:无法自动检测 OpenSSL 支持

html - Ubuntu 上的 node-html-pdf 名片示例

node.js - Sequelize Tables 不相关的问题

javascript - Node.js 中如何处理互包含?

javascript - 返回空数组或错误