gpt4 book ai didi

javascript - Electronjs Mysql数据库提交查询随机访问

转载 作者:行者123 更新时间:2023-12-03 12:38:44 27 4
gpt4 key购买 nike

我在与mysql交互时遇到一些麻烦问题。
我有一个表格。当我尝试单击/提交,然后调用我的函数tata()对数据库进行某些操作(选择或其他操作)时,会随机调用connection.query。有时,当我第一次单击函数调用时,有时我需要在按钮上单击2或3次才能调用该函数。我没有警告或错误的日志。该函数在connection.query之前停止,但我不知道为什么,因为我只需要重试(重新单击)即可。
首次点击示例:

"Click myBtn"

alert('14.1')
alert('13')
然后完成。
如果我停留在同一页面但又单击另一次->
alert('14.1')
alert('13')
alert('14.2')
我在Ubuntu 20上工作。
我的main.js:
const { app, BrowserWindow, netLog, contentTracing, session } = require('electron')

function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
title: "Ulrich The Keeper",
icon: './src/webapp/public/assets/img/menu_icon.png',
webPreferences: {
//set to true to have internal nodejs function integration
nodeIntegration: true,
//enable remote module for the render script
enableRemoteModule: true,
//disable dev tools set to false in PROD
devTools: true


}
}
)
win.loadFile('index.html');

}

app.whenReady().then(createWindow)

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (win === null) {
createWindow()
}
})
我的package.json
  "name": "test",
"version": "1.0.0",
"description": "test",
"main": "main.js",
"scripts": {
"postinstall": "install-app-deps",
"start": "electron . --no-sandbox"
},
"author": "",
"license": "ISC",
"devDependencies": {
"decache": "^4.6.0",
"electron": "^10.1.4",
"electron-builder": "^22.9.1"
},
"dependencies": {
"mysql": "^2.18.1"

}
}
我的index.html
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ulrich The Keeper</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">


<link rel="stylesheet" href="../assets/css/login_style.css">

</head>

<body>

<div class="login-dark">
<form id="myForm_index" method="POST">
<h2 class="sr-only">Login Form</h2>
<div class="illustration"><i class="icon ion-ios-locked-outline"></i></div>
<div class="form-group"><input id=form_index_login class="form-control" type="text" name="login" placeholder="Login" required></div>
<div class="form-group"><input id=form_index_password class="form-control" type="password" name="password" placeholder="Password" aria-describedby="PasswordHelp" required></div>
<small id="PasswordHelp" class="form-text text-muted">Enter your credentials to unlock your vault </small>

<div class="form-group">
<button id=myBtn class="btn btn-primary btn-block" type="submit" >Log In</button>
</div>
<a href="register.html" class="forgot">Click here to register</a>

</form>
</div>
<!-- Placing scripts at the bottom of the <body> element improves the display speed, because script interpretation slows down the display. -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.bundle.min.js"></script>

<script>
document.getElementById("myBtn").addEventListener("click", tata);

function tata(){
var mysql = require("mysql");
var db_config = require('../../../server/database_config.js');

var host = db_config.db_host;
var user = db_config.db_user;
var password = db_config.db_password;
var name_database = db_config.db_name;

var connection = mysql.createConnection({
host: host,
user: user,
password: password,
database: name_database
});
alert('14.1')
connection.query('SELECT * FROM `users`', function (error, results, fields) {
if (error) throw console.log(error);
alert('14.2');
});
alert('13')
connection.end();

}
</script>
</body>
</html>

最佳答案

我想出了解决方案。
我将“type = submit”移到“type = button”上了!
我不知道为什么会这样,但是确实可行。
如果您有任何解释,请随时!

<div class="form-group">
<button id=myBtn class="btn btn-primary btn-block" type="button" >Log In</button>
</div>

关于javascript - Electronjs Mysql数据库提交查询随机访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64801527/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com