Search code examples
node.jsmariadb

MariaDB upload with Node.js stops after 10 records


I'm facing an issue with MariaDB and Node.js.

When I try to upload data from bluelinky with Node.js to MariaDB, the upload stops after exact 10 (ten) records, no matter if there are 11 or 17.

The missing I add manually.

Before I add the code here, are you aware of some limitation when using Node.js and MariaDB?

Thanks moses

Update 09.08.2023: @Bagus Tesa: I added the code how I get the data.

The console output looks like this:

2023-08-09, 4, 71, 56.5, 116, 62, 112, 08:03:17, 08:44:17, 41, 5, 35, Wednesday 3
2023-08-09, 4, 71, 56.5, 116, 48, 91, 17:36:23, 17:59:23, 23, 6, 13, Wednesday 2
2023-08-09, 4, 71, 56.5, 116, 63, 116, 18:30:51, 18:47:51, 17, 1, 15, Wednesday 1
2023-08-09, 4, 71, 56.5, 116, 53, 110, 19:12:19, 19:22:19, 10, 0, 8, Wednesday 0

However When I have more then ten records, maximum 10 will be uploaded. The problem is, there is no error, it stops for no reason after 10 records

Code to extract and upload the data

// Use the MariaDB Node.js Connector and run npm install mariadb
const mariadb = require('mariadb');
const { kill } = require('process');

// Create a connection pool
const pool = 
  mariadb.createPool({
    host: 'xxx.xxx.xxx.xx', 
    port: xxxx,
    user: 'user', 
    password: 'pwd',
    database: 'db'
});


//Bluelinky verbinden
const BlueLinky = require("bluelinky");


const client = new BlueLinky({
  username: 'username',
  password: 'pwd',
  brand: 'hyundai', // 'hyundai', 'kia'
  region: 'xx', // 'US', 'EU', 'CA'
  pin: 'xx'
});


async function myLoop() {

//get yesterdays date
const repdate = new Date();
repdate.setDate(repdate.getDate()); // getDate()-1) => yesterday, blank => today 



// called when the client logs in successfully
client.on("ready", async () => {
  const vehicle = client.getVehicle("xxxxx");
  const trpInfo = await vehicle.tripInfo({year: repdate.getFullYear(), month: repdate.getMonth()+1, day: repdate.getDate()});


if (trpInfo != '') 
    {
  
  //Erstelle Anzahl der Fahrten
  const tripdayCnt = trpInfo[0].tripsCount;
  
  // Errechne Anzahl der Fahrten
  i = tripdayCnt - 1

  while(0 <= i) {

      //Erstelle Datum
      const tripdaydateraw = trpInfo[0].dayRaw.slice(4,6) + '/' + trpInfo[0].dayRaw.slice(6,8) + '/' + trpInfo[0].dayRaw.slice(0,4);
      const tripdaydate = trpInfo[0].dayRaw.slice(0,4) + '-' + trpInfo[0].dayRaw.slice(4,6) + '-' + trpInfo[0].dayRaw.slice(6,8);
      const tripdaynameraw = new Date(tripdaydateraw).toLocaleDateString('de-de', {weekday: 'long'});
      const tripdayname = tripdaynameraw;
      //Erstelle Anzahl der Fahrten
      //const tripdayCnt = trpInfo[0].tripsCount;
      //Erstelle Tagesdistanz
      const tripdayDistance = trpInfo[0].distance;
      //Erstelle Tagedurchschnittsgeschwindigkeit
      const tripdayavgSpeed = trpInfo[0].speed.avg;
      //Erstelle maximale Tagesgeschwindigkeit
      const tripdaymaxSpeed = trpInfo[0].speed.max;

      //Erstelle Durchschnittsgeschwindigkeit Fahrt
      const tripavgSpeed = trpInfo[0].trips[i].speed.avg;
      //Erstelle maximal Fahrgeschwindigkeit
      const tripmaxSpeed = trpInfo[0].trips[i].speed.max;
      //Startzeit
      const strttimstamp = trpInfo[0].trips[i].start;
      //Enzeit
      const endtimstamp = trpInfo[0].trips[i].end; 
      //Startdatum
      const strtdate = strttimstamp.toLocaleDateString();
      //toLocaleTimeString konvertiert GMT + 1, Startzeit
      const strtime = strttimstamp.toLocaleTimeString() ;
      //Enddatum
      const enddate = endtimstamp.toLocaleDateString();
      //toLocaleTimeString konvertiert GMT + 1, Endzeit
      const endtime = endtimstamp.toLocaleTimeString();

      //Fahrzeit in Minuten
      const drvtime_minutes = trpInfo[0].trips[i].durations.drive;

      //Stehzeit in Minuten
      const idletime_minutes = trpInfo[0].trips[i].durations.idle;

      //Distanz
      const drvdistance = trpInfo[0].trips[i].distance;

      //Tankrechnung
      const refuel_amt = '0.00'

      //Werte Ausgeben
      const tripinfodata = tripdaydate + ', ' + tripdayCnt + ', ' + tripdayDistance + ', ' + tripdayavgSpeed + ', ' + tripdaymaxSpeed + ', ' + tripavgSpeed + ', ' + tripmaxSpeed + ', ' + strtime + ', ' + endtime + ', ' + drvtime_minutes + ', ' + idletime_minutes + ', ' + drvdistance + ', ' + tripdayname;
      
      //SQL Statement bauen
      let connection

      try {
        connection = await pool.getConnection()
        } catch(error) {
          //not connected
          pool.end()
          return // or: log and/or throw error again
        }

      // run insert query
      const queryString = "INSERT INTO nightfury_trips (tripdaydate, tripdayCnt, tripdayDistance, tripdayavgSpeed, tripdaymaxSpeed, tripavgSpeed, tripmaxSpeed, strtime, endtime, drvtime_minutes, idletime_minutes, drvdistance, tripdayname, refuel_amt) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?);"
      const queryData = [tripdaydate, tripdayCnt, tripdayDistance, tripdayavgSpeed, tripdaymaxSpeed, tripavgSpeed, tripmaxSpeed, strtime, endtime, drvtime_minutes, idletime_minutes, drvdistance, tripdayname, refuel_amt]

      let rowsInserted
      try {
        rowsInserted = connection.query(queryString, queryData)
      } catch (error) {
        // handle error
        console.log(error);
        connection.end();
        
        // continue on the next loop
        i--;
        continue;
      }
  
      // rows inserted successfully, log them
      console.log(rowsInserted)
      console.log(tripinfodata, i)
      
      i--;
    }}
  
    pool.end()
    console.log('finished!')

  })}

// call myLoop.
// this is an async function
myLoop()
  .then(() => console.log('myLoop finished!'))
  .catch((error) => console.error('unexpected error:', error))

Solution

  • Per accident I ran into the solution after seven month: Had to add "connectionLimit: 100" to the connection. Thanks to all who answered to my question.