Não estou a conseguir por a funcionar o a verificar se o utilizador ja existe dimanicamente basicamente quero que quando se vai digitando o nome do utilizador diz se existe ou não estou a usar o nodejs com o express e mysql
codigo 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)
}
}
})
})
}
validador
rules : {
username
minlength : 3,
required : true,
remote : {
url : '../controllers/login',
message : 'Utilizador ja existe',
data: JSON.stringify('Username'),
type : 'POST',
contentType: 'application/json',
},
}