I'm trying to create an array of items from a MySQL query to return from an async function but the returns are empty.
Things I've tried:
- Read up to the latest info about async/await
- Stack overflow
- Numerous test around changing code, replacing return calls, rewriting functions.
This is for a new webservice (nodejs) that needs to initialize values from a MySQL database and after that fast access to the values to compare them against values pulled from the internet. To limit the amount of database calls I'm planning to have the values in an array (in-memory) and whenever they change enough (based on calculations) write them to the DB.
All is Linux based using Node 11
require('dotenv').config()
var mysql = require('mysql')
var dbconnection = mysql.createConnection({
host: 'localhost',
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PW,
database: process.env.MYSQL_DB
})
dbconnection.connect(function (err) {
if (err) {
console.error('error connecting: ' + err.stack)
}
})
async function ReadDB () {
var ArrBuyPrice = []
var query = 'SELECT * FROM pricing'
var items = await dbconnection.query(query, function (err, rows, result) {
if (err) throw err
for (var i in rows) {
ArrBuyPrice.push(rows[i].price_buy.toFixed(8))
}
return ArrBuyPrice
})
return items
}
async function InitialProcess () {
var DbResult = await ReadDB()
console.log(DbResult)
}
InitialProcess()
I would expect the console.log
output to be [ '0.00000925', '0.00000012' ]