2017-04-10 17 views
0

私は電子メールを使用しているエクスプレスアプリを使用しています。何が起こっているのは、ユーザーに空きアイテムのリストが表示され、クリックするとそのアイテムの情報を使って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'); 
     } 

あるビューは、何が起こることは一つだけのアイテムがデータベースから引き出され、ユーザにリストとして表示され、クリックするとされている場合ということです

<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); 
      }); 

ここでも、接続エラーが一つだけのアイテムがレンダリングされた状態で投げていないルート・ページ(ユーザーが項目をクリックしたら、それはこのルートページに掲載されますが)ですが、それフォーム内に複数の問題があるようです。私はその問題を推測している。

UPDATE

もっとコード

// 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

+0

フォームに複数の項目がある場合、生成されたHTML(ブラウザが表示/ソースと見なすもの)を表示できますか?ブラウザが見る実際のHTMLを見ることができます。それがテンプレートからのものかもしれない。また、最後のコードブロックのコードにつながるルートから上位レベルのコードを表示できますか? – jfriend00

+0

私の推測では、複数の項目が選択されているときに 'boxSelectedDimension'と' boxSelectedValue'はあなたが望むものではありませんが、変数の設定方法を示す十分なコードがありません。 – jfriend00

+0

boxSelectedValueとoxSelectedDimensionの取得方法を示すコードを追加しました – John

答えて

1

私はそれは、それぞれが別々の次元が設定されていることを別の形で各ボタンを配置することです解決すると考えることができます最も簡単な方法その中に。フォームのポストで取得したディメンションは、押されたボタンの唯一のものになります。

私のハンドルバーは少し錆びたかもしれませんが、おそらくこのような何か:

{{#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}} 

そして、あなたは、これが今、N、別のフォーム(それぞれに1つであるという事実に対処するためのCSSを調整する必要があるかもしれボタン)。

関連する問題