html 带有react前端的Mysql Nodejs没有从select语句返回任何数据,并且result . length返回[]

hgncfbus  于 2023-04-04  发布在  React
关注(0)|答案(1)|浏览(115)

我使用Mysql作为react应用程序的后端。我的动机是验证用户名是否已经存在于数据库中,如果不存在则返回'done',如果已经存在则返回'username exist'我的Mysql服务器(node.js)代码如下:

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") 
    });

前端: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;

**数据库:mysql文件

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    |
+--------------+----------+

我已经试过很多次了,我也试过:从skcred中选择count(用户名);如果它可以显示结果,但它里面的数据显示为[]。

j2datikz

j2datikz1#

看起来您正在执行的查询是
SELECT * FROM SKCRED WHERE username ='username'
您必须确保在请求中发送的是您的变量 username 而不是字符串

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") 
    });

相关问题