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>
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;
當窗體中有多個項目時,您可以向我們展示生成的HTML(瀏覽器在View/Source中看到的內容),因此我們可以看到瀏覽器看到的實際HTML並且不必嘗試瞭解它可能來自模板。另外,您能否向我們展示導致上一代碼塊中代碼的路由中的更高級別代碼? – jfriend00
我的猜測是'boxSelectedDimension'和'boxSelectedValue'不是你想要的,當選擇多個項目時,但我們沒有足夠的代碼來告訴如何設置這些變量。 – jfriend00
我添加了更多的代碼來顯示boxSelectedValue和oxSelectedDimension是如何被檢索的 – John