我正在使用 react 和 nodejs 制作一个项目,但是数据无法传递到数据库,因为我使用的是 mysql 这些是文件

编程入门 行业动态 更新时间:2024-10-06 06:49:59

我正在使用 react 和 nodejs 制作一个项目,但是数据无法传递到数据库,因为我使用<a href=https://www.elefans.com/category/jswz/34/1770536.html style=的是 mysql 这些是文件"/>

我正在使用 react 和 nodejs 制作一个项目,但是数据无法传递到数据库,因为我使用的是 mysql 这些是文件

app.js

import './App.css';
import {useState} from 'react';
import Axios from 'axios'


function App() {

const [password,setPassword] = useState("");
const [title,setTitle] = useState("");

const addPassword= ()=>{
  Axios.post("http://localhost:3001/addpassword",{
    password: password,
    title:title
  });
}

  return (
    <div className="App">
      <div className="AddingPassword">
        <input type="text" placeholder="Ex. password:123" onChange={(event)=>{
          setPassword(event.target.value)}}/>
        <input type="text" placeholder="Ex. Facebook" onChange={(event)=>{
          setTitle(event.target.value)}}/>
        <button onClick={addPassword}>Add Password</button>
      </div>

    </div>
  );
}

export default App;

节点

const express = require("express");
const app = express();
const mysql = require("mysql");
const cors = require("cors");
const PORT = 3001;

app.use(cors()); // this will allow use to make connection between two servers in local machine.
app.use(express.json()); //this will make the use of json file using express

const db = mysql.createConnection({
    user: "root",
    host: "localhost",
    password: "root",
    database: "passwordmanager",
})

app.post("/addpassword",(req,res)=>{
    const {password,title} =req.body;

    db.query("INSERT INTO passwords (password,title) VALUES (?,?)",[password,title],(err,result)=>{
        if(err){
            console.log(err);
        }else{
            res.send("Success");
        }
    })
})
app.listen(PORT,()=>{
    console.log("server is running")
})

我没有收到错误,但是是的,数据库没有连接,而且值也没有被推送到表中。帮助我的社区!

所以数据没有传递到 mysql 数据库。我可能正在寻找一些可能的解决方案。 ->db 没有连接

回答如下:

只需将以下行添加到您的 mysql 工作台,这是由于密码导致系统和 mysql 之间的连接问题。

在工作台中的选择行上方写下以下行,它对我有用

更改用户'root'@'localhost'由'root'标识为mysql_native_password;

更多推荐

我正在使用 react 和 nodejs 制作一个项目,但是数据无法传递到数据库,因为我使用的是 mysql 这些是文件

本文发布于:2024-05-30 16:11:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770679.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:的是   数据库   文件   因为我   项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!