I'm not being able to check whether the user already exists dynamically. Basically I want when typing the user name says whether or not I am using nodejs with express and mysql
code controller / login
const usermameverica = async (connection, req, res) => {
// req.body.password = await hashpass(req.body.password)
const user= await User.verificauser(connection, req.body.user.username)
if(user){
return res.render('login/create',{error: true})
}else{
return res.render('login/create',{error: false})
}
}
route login
const express = require('express')
const router = express.Router()
const controllerLogin = require('../controllers/login')
const controllernews = require('../controllers/newscontroler')
const connection = require('../Config/database')
const controllerAdmin = require('../controllers/Administration')
router.get('/login', controllerLogin.login)
router.post('/login', controllerLogin.authenticateUser.bind(null, connection))
router.get('/createUser', controllerLogin.FormUser)
router.post('/createUser', controllerLogin.createUser.bind(null, connection))
router.post('/usermameverica', controllerLogin.usermameverica.bind(null, connection))
router.get('/logout', controllerLogin.logoutUser)
router.get('/news', controllernews.news)
//
/*************************/
module.exports = app => app.use('/', router)
models / user
const verificauser = (connection, username) => {
return new Promise ((resolve, reject) => {
connection.query('SELECT * FROM user WHERE username = '${username}' ', (err, result) =>{
if(err){
reject(err)
}else{
if(result.length>0){
resolve(false)
}
else{
resolve(true)
}
}
})
})
}
validator
rules : {
username : {
minlength : 3,
required : true,
remote : {
url : '../controllers/login',
message : 'Utilizador ja existe',
data: JSON.stringify('Username'),
type : 'POST',
contentType: 'application/json',
},
}