user checking problem already exists in mysql nodejs

Asked

Viewed 99 times

0

I’m having trouble getting it working or checking if the user already exists dynamically.
Basically I want that when typing the user name says whether or not I am using the 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',
                                    },
}
  • what’s the matter, some mistake happens?

  • no error simply does not show which user exists or not

  • checked if you are mounting the command sql correctly? if you pick up the command sql that was generated and run directly in the bank works?

  • in the sql part works I think the problem is in jquery

  • You’ve seen what the service returns?

  • already solved topical problem can be closed

Show 1 more comment

1 answer

0


to solve the problem I did the following

fields: {
           username: {
                validators: {
                        stringLength: {
                        min: 4,
                    },
                        notEmpty: {
                        message: 'Insira o username'
                    },
                                        remote : {
                                        url : './usermameverica',
                                        message : 'Username has been taken',
                                        data : {
                                            type : 'username'
                                        },
                                        type : 'POST',
                                        success: function(data,) {
                                    valid:true
                                },
                                error: function(errorThrown) {
                                    valid:false
                                }
                                    },
          }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.