Currently, I am facing an issue where I am trying to access a value that is set in one function within another function. When I attempt to return this value at the end, the console.log displays the value correctly. However, when I try to set it, I receive an error message saying:
TypeError: Cannot read property '0' of undefined
It appears that when I try to return the value by setting the function as a variable, a function with no result is displayed. I am unsure how to display the result from my setValue function. Any suggestions? :) Here is my Express code:
function showBalance(cardNumber) {
connection.query(
"SELECT Balance from userCards WHERE CardNumber = '" + cardNumber + "'",
function(err, rows) {
if (err) {
throw err;
} else {
setValue(rows);
}
}
);
function setValue(value) {
console.log('Balance object is:', value[0].Balance);
const ownBalance = value[0].Balance;
console.log('The user balance is: ', ownBalance);
return ownBalance;
}
const returnBalance = setValue();
return returnBalance;
}
module.exports = showBalance;