I'm writing a function that pulls data out of a SQLite database, parses the information into a Javascript array and formats it into the format that the application I'm working on is expecting and pushes the new formatted data into an array to be sent to our front end. However, whenever I receive the information on the front end, I have the right array length, but it's using the last value of the array.
Here is the body of my function
EDIT: For the sake of the example, I am using the better-sqlite3 library.
var optionalParams = "";
if(area != null){
optionalParams = ` WHERE areaId = ${area}`
}
let itemsToSend = [];
for (const driver of drivers){
let table = `TABLE`
let dbReturn = database.prepare(`SELECT * FROM ${table}${optionalParams}`).all();
for(const row of dbReturn){
let item = {...driver}
item.name = row.name;
item.id = row.id;
item.area = row.name
item.instanceId = row.instanceId;
itemsToSend = [...itemsToSend, item];
}
};
return itemsToSend;
I've done enough research to know that this should work in series, so could someone explain to me why this isn't working the way please?