【问题标题】:How to make axios post to a database in ReactJS?如何使 axios 发布到 ReactJS 中的数据库?
【发布时间】:2021-09-03 11:58:10
【问题描述】:

我正在尝试用mysql+axios+express+react做一个简单的登录系统。

与我的数据库的连接进展顺利,但这两个特定帖子出现“ERR_CONNECTION_REFUSED”和 UNCAUGHT IN_PROMISE 等错误。

错误信息

POST localhost/3001/api/login net::ERR_CONNECTION_REFUSED 和未捕获(承诺)错误:在 XMLHttpRequest.handleError 的 createError (createError.js:16) 处出现网络错误

我的数据库:

我的代码: `

app.post("/api/register",(req,res) => {

    const email = email
    const senha = senha

    const sqlInsert = "INSERT INTO `alunos` (`email`,`senha`) VALUES (?,?);"
    db.query(sqlInsert,[email,senha],(err,result)=>{
        res.send(result);
    });
});

app.post("/api/login",(req,res) => {

const email = email
const senha = senha

db.query("SELECT * FROM  `alunos` WHERE email = ? AND senha = ?;",[email,senha],
    (err,result) => {
        if(err){
            res.send({err:err});
        }
        
        if(result.length>0){
            res.send(result);
        }else{
            res.send({message: "COMBINAÇÃO ERRADA."});
        }
    }
)});

const register = () => {
    Axios.post('https://localhost/3001/api/register',{
        email: "testmail",
        senha: "testpassword",
    }).then((response)=>{
        console.log(response);
    });
}

const login = () => {

    Axios.post('https://localhost/3001/api/login',{
        email: "testmail",
        senha: "testpassword",
    }).then((response)=>{
        if(response.data.message){
            setLoginStatus(response.data.message)
        }else{
            setLoginStatus(response.data[0])
        }
    });
}`

怎么了?我真的认为这是我的 mysql 语句有问题,因为其他 mysql 语句工作正常。

【问题讨论】:

  • 请不要以纯文本形式保存密码,即使是用于学校项目。你到底得到了哪些错误
  • 这看起来是网络错误而不是代码问题,你检查错误日志吗
  • @nbk 这不是网络问题,因为其他帖子工作正常。我真的认为这是我编写语句“插入.....”的方式有问题,但我无能为力
  • 你检查过 db 有一个有效的连接吗?
  • @nbk 怎么做?

标签: javascript mysql reactjs axios


【解决方案1】:

成功了。我使用的是 '' 而不是 ""

【讨论】:

    猜你喜欢
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    相关资源
    最近更新 更多