Reputation: 51
I am using express node.js with mysql to build the api's and want to connect these api's to the front end but i have face an error, due to this my application didn't run properly plzzzz tell me whats the problem in my code.
And my error is : Error: 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
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]);
});
})
);
};
Upvotes: 5
Views: 31725
Reputation: 9696
For what it's worth, with mysql you can also specify the database as part of the connection setup: https://github.com/mysqljs/mysql#introduction
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'coffee_shop'
});
So you would not have to perform the 'USE' query in a separate call.
Upvotes: 0
Reputation: 3066
The error is pretty self explanatory buddy.
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
This code: connection.query('USE' + dbconfig.database);
translates to connection.query('USEcoffee_shop');
But SQL doesn't recognize USEcoffee_shop
as a valid syntax. There has to be a blank space between USE
and the Db name.
Hence modify your code to:
connection.query('USE ' + dbconfig.database); //observe the space after USE
This should work.
Upvotes: 5
Reputation: 2434
Wrong code: connection.query('USE' + dbconfig.database);
Right code: connection.query('USE ' + dbconfig.database);
You missed a space after the keyword USE
Upvotes: -1