node.js - 通过Express应用程序连接错误

标签 node.js express mysqli handlebars.js electron

我正在使用通过 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>
                &nbsp;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;

enter image description here

最佳答案

我能想到的最简单的解决方法是将每个按钮以单独的形式放置,每个按钮都设置有单独的尺寸。然后,您在表单发布中获得的尺寸将是所按下按钮的唯一尺寸。

我的 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>
            &nbsp;Longest dimension {{this.longestDimension}}"
        </button>
    </form>
{{/each}}  

并且,您可能必须调整CSS来处理这是N个单独的表单(每个按钮一个)的事实。

关于node.js - 通过Express应用程序连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43313236/

相关文章:

javascript - 使用 Javascript/Node.js 在代码中执行 mongoimport

node.js - gulp 默认值,任务从未定义 : default, 请检查文档以了解正确的 gulpfile 格式

node.js - 创建添加到数组字段的新子项

node.js - 如何在 express 网站中使用node的集群模块?

javascript - API 调用时获取无效 JSON

php - 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行 '1' 附近使用的正确语法

javascript - Heroku 部署错误 : Cannot get Node App running after Deploy : Cannot find module '/app/web.js'

javascript - npm:创建脚本命令

php - MySQLi (PHP) 中的绑定(bind)参数和转义字符串

php update ,更新一些值双倍