有什么方法可以在JavaScript中插入基于引用的数字组找到的最大数字?

问题描述 投票:0回答:1

问题:目前,我正在运行一个循环,将数值插入到MySQL DB中。在这个循环中,我正在生成一个MAX(num),以获得所选的每个商店组中的最高数字。然后,该应用程序创建了一个包含多个值的列表,其中包括该Max数字。 RowDataPacket {MAX(Offset): 1} 在我的控制台日志中出现了MAX(num)。然而,当INSERT查询运行时,DB的更新值为 [object Object] 而不是该商店的最大ID值。下面我复制了我目前正在研究的函数。任何帮助将是感激的。如果需要更多的信息,请告诉我,提前感谢。

function alertStoresByDc(){
  var returnedStoresbyDC = [];
  /**This method will return all IPs associated with the store number */           
  var db       = require('../app/model/connect_db');
  var mysql    = require('mysql');  
  var date     = new Date();      //Get todays date, and formats it into the following (yyyy/mm/dd/hh/mm/ss) for MySQL
  var today    =  date.toISOString().split('T')[0] + ' ' + date.toTimeString().split(' ')[0]; 
  var dcOpt    = document.getElementById("sendOptionDC");     //Getting the selected dc, then storing it into a variable
  var dcNumber = parseInt(dcOpt.options[dcOpt.selectedIndex].value);
  let message  = document.getElementById("alert-message").value;       //Get alert message from user input, then stores it into a variable 
  var sender   = 427750;

  //Confirms with the user before sending the alert message
  var r = confirm("Are you sure you want to send this alert? Press ok to confirm.");
  if (r==true) {
    //Gets store name, ip, and dc number 
    $queryStoresByDC = 'SELECT `StoreNumber` FROM `store` WHERE DCNum = ' + mysql.escape(dcNumber) + '; '
    db.query($queryStoresByDC, (err, rows) => {
      if(err)
        return console.log("An error occured with the query", err);
      }
      //Data returned from the MySQL database will be parsed by looping over the rows object. getting IP addresses, then adding to a list  
      rows.forEach( (row) => {
        console.log(`${row.StoreNumber}`);
        returnedStoresbyDC.push(`${row.StoreNumber}`); //Adding store numbers to the returnedStoresbyDC list
        document.getElementById("messageSentBoxInfo").innerHTML = "Alert message sent to the following stores in DC " + mysql.escape(dcNumber) + ": \n" + "Stores: " + returnedStoresbyDC; 
      });                                
      console.log("List of Stores: " + returnedStoresbyDC);

      var records = [];
      //Adds the message, store number, and todays date into the records array list for the insert statement
      returnedStoresbyDC.forEach((storeNum) => {

        //Getting max offset for current store    
        $queryOffsetByDC = 'SELECT MAX(Offset) FROM `messages_sent` WHERE `StoreNum` = ' + mysql.escape(storeNum) + '; '
        db.query($queryOffsetByDC, (err, offs) => {
          if(err){
            return console.log("An error occured with the query", err);
          }
          console.log(offs);
          records.push([message, storeNum, sender, offs, today]); 
          console.log(records);
          var sql = "INSERT INTO `messages_sent`(Message, StoreNum, AdminSender, Offset, DateSent) VALUES ?";
          db.query(sql, [records], (err) => {
            if (err) {
              return console.log("An error occured with the query", err);
            }
          });                    
        }); 
      }); 

    });  
    //Calling method to send kafka message by DC
    sendKafkaMsgByDC();
    txt = "Alert sent!";   
  }
  else{
    txt = "No alert update sent";
  }
  document.getElementById("msgOutcome").innerHTML = txt; 
}
javascript mysql node.js max
1个回答
0
投票

我想明白了,我给了MAX(Offset)。我给MAX(Offset)数字一个标识符maxOffset,然后用下面的内容引用它。 "offs[0].maxOffset".

            //Getting max offset for current store    
            $queryOffsetByDC = 'SELECT MAX(Offset) as maxOffset FROM `messages_sent` WHERE `StoreNum` = ' + mysql.escape(storeNum) + '; '
            db.query($queryOffsetByDC, (err, offs) => {
                if(err){
                    return console.log("An error occured with the query", err);
                }
                console.log('Max Offset: ' + offs[0].maxOffset);
                records.push([message, storeNum, sender, parseInt(offs[0].maxOffset) + 1, today]); 
                //Inserts records with associated offset number
                var sql = "INSERT INTO `messages_sent`(Message, StoreNum, AdminSender, Offset, DateSent) VALUES ?";
                db.query(sql, [records], (err) => {
                    if (err) {
                        return console.log("An error occured with the query", err);
                    }
                });  

            }); 
© www.soinside.com 2019 - 2024. All rights reserved.