[英]perl lwp useragent and mysql data download and update character set trouble
[英]UPDATE and SET trouble in nodejs (MYSQL)
我正在嘗試使用 node.js 將數據庫上的狀態更新為 1
這是我的代碼
app.post('/voted', (req, res) => {
const email = req.body.email
const num = 1;
db.query("UPDATE voter SET status = ? WHERE email = ?",
[num, email],
(err, result) => {
if(err) {
console.log(err)
} else {
res.send( {voted: true } )
console.log(result)
}
})
})
我得到的 console.log:
OkPacket {
fieldCount: 0,
affectedRows: 0,
insertId: 0,
serverStatus: 2,
warningCount: 0,
message: '(Rows matched: 0 Changed: 0 Warnings: 0',
protocol41: true,
changedRows: 0
}
idk 我的 go 哪里錯了,我的 SQL 結構不正確嗎?
--- 更新 --- 我發現了問題,似乎我必須在查詢正確注冊之前雙擊按鈕
// First Click
OkPacket {
fieldCount: 0,
affectedRows: 0,
insertId: 0,
serverStatus: 2,
warningCount: 0,
message: '(Rows matched: 0 Changed: 0 Warnings: 0',
protocol41: true,
changedRows: 0
} // 2nd Click
OkPacket {
fieldCount: 0,
affectedRows: 1,
insertId: 0,
serverStatus: 2,
warningCount: 0,
message: '(Rows matched: 1 Changed: 1 Warnings: 0',
protocol41: true,
changedRows: 1
}
我認為問題出在我的 handleStatus function
const [userEmail, setEmail] = useState('')
const handleStatus = () => {
Axios.get("http://localhost:3001/login").then((response) => {
if (response.data.loggedIn === true) {
setEmail(response.data.user[0].email)
}
})
Axios.post('http://localhost:3001/voted',
{email: userEmail}).then((response) => {
console.log(response)
})
}
```
您可以嘗試使用此代碼嗎?
const syncSql = require('sync-sql');
const email = req.body.email
const num = 1;
const con = {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
};
const query = `UPDATE voter SET status = ? WHERE email = ?`
const data = { status: num, email }
const response = syncSql.mysql(con, query, data)
如果您使用的是本地數據庫,那么
UPDATE,謝謝大家的回答我已經找到了問題
我做了這個
const [userEmail, setEmail] = useState('')
useEffect(() => {
Axios.get("http://localhost:3001/login").then((response) => {
if (response.data.loggedIn === true) {
setEmail(response.data.user[0].email)
}
})
}, [])
const handleStatus = () => {
Axios.post('http://localhost:3001/voted',
{email: userEmail}).then((response) => {
console.log(response)
})
}
而不是這個
const [userEmail, setEmail] = useState('')
const handleStatus = () => {
Axios.get("http://localhost:3001/login").then((response) => {
if (response.data.loggedIn === true) {
setEmail(response.data.user[0].email)
}
})
Axios.post('http://localhost:3001/voted',
{email: userEmail}).then((response) => {
console.log(response)
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.