我正在使用express Node.js和mysql来构建API,并且想要将这些API连接到前端,但是我遇到了一个错误,由于这个原因,我的应用程序无法正常运行plzzzz告诉我代码中的问题是什么。
我的错误是:error: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:您的SQL语法中有一个错误;检查与MariaDB服务器版本相对应的手册,以确定在第1行'usecoffee_shop'附近使用的正确语法
以下代码:
但SQL无法将
因此将代码修改为:
connection.query('USE ' + dbconfig.database); //observe the space after USE
这应该管用。
错误代码:
正确代码:
关键字USE后面缺少空格
在mysql中,您还可以指定数据库作为连接设置的一部分:https://github.com/mysqljs/mysqlintroduction
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'coffee_shop'
});
因此您不必在单独的调用中执行“use"查询。