Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mysql Nodejs with react front end is not returning any data from select statement, and result . length returning []

I have used Mysql as backend for react application. my motive was to verify if username already exist in the database and return ‘done’ if not exist and return ‘username exist’ if already exists
my** Mysql server (node.js) code **is as follows:

const cors=require("cors");
    const { response } = require("express");
    const express = require("express");
    const app = express();
    const mysql=require("mysql");
    app.use(cors());
    app.use(express.json());
    const db=mysql.createConnection({
        host: "localhost",
        user: "root",
        password:"password",
        database:"ggsklogin",
    });
app.post("/checkusersk",(req,res)=>{
        const username=req.body.usernamea;
    db.query("select username from skcred where username=username;",
    (err,results)=>{
        if(err){
            res.send({err});
        }
         if(results.length>0){
            res.send(results);

        }
        else{   
            res.send({message:"done"});
        }
       
        
    });
    });
app.listen(3001,()=>{
        console.log("running on 3001") 
    });  



front end:
newlogin.jsx
:

import React, { Fragment } from 'react';
import { useState,useNavigate,useEffect} from 'react';
import axios from 'axios';
function Newlogin() {
    const[usernamea,setusername]=useState('')
    const[password,setpassword]=useState('')
    const[repeatpass,setreapeatpass]=useState('')
    const[loginstatus,setLoginstatus]=useState('')
    const check=()=>{
    axios.post('http://localhost:3001/checkusersk', {
           username: usernamea,
        }).then((response)=>{
            if(response.data){
            setLoginstatus(response.data.message);}
            else{
                setLoginstatus(response.data[0].username)
            }
            
        })

        }
    return ( 
        <div>
        <div>
            <input type="text" placeholder='enter username' name='username' onChange={(e)=>{setusername(e.target.value)}}/>
            <input type="password" placeholder='enter password' name='password' onChange={(e)=>{setpassword(e.target.value)}}/>
            <input text="password" placeholder='repeat password' name='repeatpass' onChange={(e)=>{setreapeatpass(e.target.value)}}/>
    
            <button onClick={check}></button>
        </div>

        <div>
            <h5>{loginstatus}</h5>
        </div>
      
        </div>
     );
}

export default Newlogin;

database : mysql

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

select * from skcred;
+--------------+----------+
| username     | password |
+--------------+----------+
| asadasasd    | dsaad    |
| asdd         | dasd     |
| asdd         | dasd     |
| sada         | ads      |
| sada         | ads      |
| sada         | ads      |
| sada         | ads      |
| sada         | ads      |
| sada         | ads      |
| sada         | ads      |
| sdaaaaaaaaaa | aaaa     |
| abiinaya     | sridhar1 |
| abinayaa     | assf     |
| abinayaa     | assf     |
| asdf         | asdf     |
| asdf         | asdf     |
| asdf         | asdf     |
| assa         | werr     |
| dddddd       | ddddd    |
+--------------+----------+

i have tried to debug this a lot of times. i have also tried :
select count(username) from skcred;
if it could display results, but the data inside it is showing as [].

>Solution :

Looks like the query you area executing is

SELECT * FROM SKCRED WHERE username=’username’

You have to make sure that it is your variable username and not the string that is sent in the request

const cors=require("cors");
    const { response } = require("express");
    const express = require("express");
    const app = express();
    const mysql=require("mysql");
    app.use(cors());
    app.use(express.json());
    const db=mysql.createConnection({
        host: "localhost",
        user: "root",
        password:"password",
        database:"ggsklogin",
    });
app.post("/checkusersk",(req,res)=>{
        const username=req.body.username;
    db.query(`select username from skcred where username=${username};`,
    (err,results)=>{
        if(err){
            res.send({err});
        }
         if(results.length>0){
            res.send(results);

        }
        else{   
            res.send({message:"done"});
        }
       
        
    });
    });
app.listen(3001,()=>{
        console.log("running on 3001") 
    }); 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading