I am getting the error below:
Service.getUsers is not a function
I recently changed my code to include this functionality:
async function connectToDatabase(secret) {
try {
const secretValue = await getSecretPromise(secret)
const { host, port, username, password, dbname } = JSON.parse(secretValue);
con = mysql.createConnection({
host: host,
port: port,
user: username,
password: password,
database: dbname
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected to database!");
});
} catch (error) {
console.error(error)
}
}
connectToDatabase(secretName).then(function() {
module.exports = {
createUser,
getUsers,
patchUser,
loginUser,
updateCallForward,
getEmail,
getCallForwardingNumber,
};
});;
Basically, it will only export the functions to the controller if the database is connected and the value of con is initialized (this ensures that the value of con is not undefined which has been troubling me). However, when I do this, it messes with the controller js and I get the error above.
var accountService = require('./account.service');
router.get('/get-users', getUsers);
async function getUsers(req, res, next) {
accountService.getUsers().then(function(val) {
res.json(val)
});
}
I believe this is because of the waiting that is employed in the service.js. Is there anyway I can make the controller wait for the functions to get initialized through the export?