我正在使用通过 Electron 运行的快速应用程序。发生的是,向用户显示了可用项目的列表,并且在单击后,它将使用该项目的信息运行python代码。
以下是路线
let sqlSelectBoxInformation = "SELECT DISTINCT longestDimension, box_id from box WHERE occupied ='unoccupied'";
connectionBoxInformation.query(sqlSelectBoxInformation, function(err, rows, fields) {
if (!err) {
// Check to see if the user entered hashtag is found in the database
// Create a variable to track if the item was found
if(rows.length > 0){
var wasFound = false;
// if (databaseHashtag == userEnteredHashtag) {
console.log(databaseHashtag);
var data = {
rows: rows,
userHashtag: databaseHashtag
}
res.render('delivery/chooseBox', data);
// Change the variable to true
wasFound = true;
}
else {
res.render('delivery/alloccupied');
}
下面是 View
<h3>Please begin by selecting the box size below:</h3>
<!-- add if statement-->
<form method="post" action="/delivery/chooseBoxSelected">
<input type="hidden" name="userHashtag" value="{{userHashtag}}">
{{#each rows}}
<input type="hidden" name="boxSelectedValue" value="{{this.box_id}}">
<input type="hidden" name="boxSelectedDimension" value="{{this.longestDimension}}">
<button class="btn-dimension" type="submit">
<i class="fa fa-cube" aria-hidden="true"></i>
Longest dimension {{this.longestDimension}}"
</button>
{{/each}}
发生的情况是,当仅从数据库中提取一项并将其作为列表显示给用户时,单击该项目即可。当从数据库中提取多个项目并将其显示给用户时,它将运行连接错误。
以下是路线页面(一旦用户单击某个项目,它就会发布到此路线页面)
let sql = `SELECT box_id, cubby_id, occupied, comport
FROM box
WHERE longestDimension = ?
AND LOWER(box_id) = LOWER(?)`;
connection.query(sql, [boxSelectedDimension, boxSelectedValue] , function(err, rows, fields) {
if (!err) {
for(var i=0; i< rows.length; i++) {
// Make the comparaison case insensitive
if (rows[i].occupied == `unoccupied`) {
console.log("unoccupied");
var comport = rows[i].comport;
var command = "open" + rows[i].cubby_id;
var commandClose = "close" + rows[i].cubby_id;
console.log(command);
console.log(comport);
var options = {
scriptPath: 'python/scripts',
args: [command, comport, commandClose] // pass arguments to the script here
};
PythonShell.run('controlLock.py', options, function (err, results) {
if (err) throw err;
console.log('results: %j', results);
});
同样,仅呈现一个项目就不会引发连接错误,但是似乎表单中存在多个问题。我猜这个问题已经解决了。
更新
更多代码
// Using post instead of get because a form was submitted with the method post
router.post('/', function(req, res){
// Store the box location in the form of box_id
var boxSelectedValue= req.body.boxSelectedValue;
var boxSelectedDimension = req.body.boxSelectedDimension;
var userHashtag = req.body.userHashtag;
最佳答案
我能想到的最简单的解决方法是将每个按钮以单独的形式放置,每个按钮都设置有单独的尺寸。然后,您在表单发布中获得的尺寸将是所按下按钮的唯一尺寸。
我的 Handlebars 可能有些生锈,但也许是这样的:
{{#each rows}}
<form method="post" action="/delivery/chooseBoxSelected">
<input type="hidden" name="userHashtag" value="{{userHashtag}}">
<input type="hidden" name="boxSelectedValue" value="{{this.box_id}}">
<input type="hidden" name="boxSelectedDimension" value="{{this.longestDimension}}">
<button class="btn-dimension" type="submit">
<i class="fa fa-cube" aria-hidden="true"></i>
Longest dimension {{this.longestDimension}}"
</button>
</form>
{{/each}}
并且,您可能必须调整CSS来处理这是N个单独的表单(每个按钮一个)的事实。
关于node.js - 通过Express应用程序连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43313236/