За последние 24 часа нас посетил 20231 программист и 1079 роботов. Сейчас ищут 770 программистов ...

Почему то логика не срабатывает и query равно ничему

Тема в разделе "JavaScript и AJAX", создана пользователем riaron, 16 сен 2020.

  1. riaron

    riaron Активный пользователь

    С нами с:
    1 окт 2014
    Сообщения:
    247
    Симпатии:
    4
    почему условие не срабатывает? я вывожу console.log(query) пишет unsigned
    Код (Javascript):
    1. app.get('/smart', function(req, res) {
    2.     var order;
    3.     var side;
    4.     var query;
    5.     var smartphopneList=[];
    6.     order='priсe';
    7.     side='ASC';
    8.     // Connect to MySQL database.
    9.     var connection = getMySQLConnection();
    10.     connection.connect();
    11.     // Do the query to get data.
    12.     if((order=='name') && (side =='ASC')){
    13.         query='SELECT `id`,`prod`,`name`,`description`,`photo`,`price`,`popularity`,`sale` FROM `smartphone` ORDER BY `name` ASC';
    14.     }else if((order=='name') && (side=='DESC')){
    15.         query='SELECT `id`,`prod`,`name`,`description`,`photo`,`price`,`popularity`,`sale` FROM `smartphone` ORDER BY `name` DESC';
    16.     }else if((order=='price') && (side=='ASC')){
    17.         query='SELECT `id`,`prod`,`name`,`description`,`photo`,`price`,`potypulari`,`sale` FROM `smartphone` ORDER BY `price` ASC';
    18.     }else if((order=='price') && (side=='DESC')){
    19.         query='SELECT `id`,`prod`,`name`,`description`,`photo`,`price`,`popularity,`sale`` FROM `smartphone` ORDER BY `price` DESC';
    20.     }else if((order=='popularity') && (side=='ASC')){
    21.         query='SELECT `id`,`prod`,`name`,`description`,`photo`,`price`,`popularity`,`sale` FROM `smartphone` ORDER BY `popularity` ASC';
    22.     }else if((order=='popularity') && (side=='DESC')){
    23.         query='SELECT `id`,`prod`,`name`,`description`,`photo`,`price`,`popularity`,`sale` FROM `smartphone` ORDER BY `popularity` DESC';
    24.     }else if((order=='sale') && (side=='DESC')){
    25.         query='SELECT `id`,`prod`,`name`,`description`,`photo`,`price`,`popularity`,`sale`,(`price` / 100 * `discount`)  AS `tsale` FROM `smartphone` ORDER BY `newprice` ASC';
    26.     }else if((order=='sale') && (side=='DESC')){
    27.         query='SELECT `id`,`prod`,`name`,`description`,`photo`,`price`,`popularity`,`sale`,(`price` / 100 * `discount`)  AS `tsale` FROM `smartphone` ORDER BY `newprice` DESC';
    28.     }
    29.     console.log("query: "+query+" order: "+order+" side: "+side);
    30.     connection.query(query, function(err, rows, fields) {
    31.           if (err) {
    32.               res.status(500).json({"status_code": 500,"status_message": "internal server error"});
    33.           } else {
    34.               // Loop check on each row
    35.               for (var i = 0; i < rows.length; i++) {
    36.  
    37.                   // Create an object to save current row's data
    38.                   var smartphopne = {
    39.                       'id':rows[i].id,
    40.                       'prod':rows[i].prod,
    41.                       'name':rows[i].name,
    42.                       'photo':rows[i].photo,
    43.                       'description':rows[i].description,
    44.                       'price':rows[i].price,
    45.                       'len':rows.length,
    46.                     'tsale':rows.tsale,
    47.                     'popularity':req.cookies.sidepopularity,
    48.                     'pricee':req.cookies.sideprice,
    49.                     'sale':req.cookies.sidesale,
    50.                     'sett':req.cookies.sett,/**/
    51.                      
    52.                   }
    53.                   // Add object into array
    54.                   smartphopneList.push(smartphopne);
    55.           }
    56.        
    57.           // Render index.pug page using array
    58.           res.render('index', {"smartphopneList": smartphopneList});
    59.           }
    60.     });
    61.  
    62.     // Close the MySQL connection
    63.     connection.end();
    64.    
    65. });
    66.  
    67. ///
    68. /// HTTP Method    : GET
    69. /// Endpoint    : /person/:id
    70. ///
    71. /// To get specific data of person based on their identifier.
    72. ///
    73. app.get('/somesmart', function(req, res) {
    74.     var somesmartlist = [];
    75.  
    76.     // Connect to MySQL database.
    77.     var connection = getMySQLConnection();
    78.     connection.connect();
    79.  
    80.     // Do the query to get data.
    81.     connection.query('SELECT `prod`,`name`,`sim`,`display`,`razresh`,`acum`,`proc`,`oper`,`sd`,`cam`,`gps`,`description`,`giroscope`,`payment`,`photo`,`price`,`webs`,`os`,`rom` FROM `smartphone` WHERE `id`='+ mysql.escape(req.params.id), function(err, rows, fields) {
    82.  
    83.           if (err) {
    84.               res.status(500).json({"status_code": 500,"status_message": "internal server error"});
    85.           } else {
    86.               // Check if the result is found or not
    87.              
    88.                   // Create the object to save the data.
    89.                   var smart = {
    90.                     'id':rows[0].id,
    91.                     'prod':rows[0].prod,
    92.                     'name':rows[0].name,
    93.                     'sim':rows[0].sim,
    94.                     'display':rows[0].display,
    95.                     'razresh':rows[0].acum,
    96.                     'acum':rows[0].acum,
    97.                     'proc':rows[0].proc,
    98.                     'oper':rows[0].oper,
    99.                     'sd':rows[0].sd,
    100.                     'cam':rows[0].cam,
    101.                     'gps':rows[0].gps,
    102.                     'description':rows[0].description,
    103.                     'giroscope':rows[0].giroscope,
    104.                     'payment':rows[0].payment,
    105.                     'photo':rows[0].photo,
    106.                     'giroscope':rows[0].giroscope,
    107.                     'price':rows[0].price,
    108.                     'webs':rows[0].webs,
    109.                     'os':rows[0].os,
    110.                     'rom':rows[0].rom,
    111.                 }
    112.                 somesmartlist.push(smart);
    113.  
    114.                   // render the details.plug page.
    115.                   res.render('one', {"somesmartlist": somesmartlist});
    116.              
    117.           }
    118.     });
    119.  
    120.     // Close MySQL connection
    121.     connection.end();
    122. });