When I call fetchLowestBin()
with a for loop for each item in items, everything is fine until it gets to db.all()
. I want to be able to wait for that snippet of code to finish, and then run console.log(itemData)
after it's done. Right now when I try running it, it will print out undefined before it even starts to print what's in the rows.forEach()
. I'm more of a beginner with JavaScript/nodejs, so sorry for the terrible code.
let db = new sqlite3.Database('./db/ahdata.db', (err) => {
if (err) {
console.error(err.message)
}
console.log(chalk.bgGreenBright('SUCCESS') + chalk.greenBright(' Connected to database.'))
db.run('CREATE TABLE IF NOT EXISTS auctions (auction_uuid TEXT,formatted_uuid TEXT,player_uuid TEXT,item_name TEXT,item_lore TEXT,price INTEGER,start_time INTEGER,end_time INTEGER,catagory TEXT,rarity TEXT,bin INTEGER);')
})
function someFunction() {
[...]
items = ['Hyperion', 'Aspect of the Dragons']
for (i in items) {
item = items[i]
fetchLowestBin(item)
}
}
function fetchLowestBin(item) {
itemData = []
db.all(`SELECT * FROM auctions WHERE item_name LIKE "%${item}%" ORDER BY price`, (error, rows) => {
rows.forEach((row) => {
if (row.bin != 1) {
return;
}
itemData.push(row);
console.log(row.formatted_uuid, timeConverter(row.end_time), row.price, row.item_name);
});
})
console.log(itemData)
}