的是 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 这些是文件
发布评论