I have deployed my Node.js project on Railway using GitHub. The app is deployed successfully with the remote MySQL database. Just after deployment the app works fine and database is queried successfully but when the app is left idle for sometime and then the database is queried the app got crashed because the results array of JS object generated from the query remains undefined and i am accessing its length property using the dot operator.
The code to the whole app.
Martyrs-Welfare-Donation-System-MWDS/controllers/donor_auth.js where the error is generated -
db.query('SELECT * FROM donors WHERE email = ?', [email], async (error, results) => {
console.log(results);
if (results.length==0 || !(await bcrypt.compare(password, results[0].password))) {
res.status(401).render('donor/donorlogin', {
message: 'Email or Password is incorrect'
})
} else {
const id = results[0].id;
const token = jwt.sign({ id }, process.env.JWT_SECRET, {
expiresIn: process.env.JWT_EXPIRES_IN
});
console.log("The token is: " + token);
const cookieOptions = {
expires: new Date(
Date.now() + process.env.JWT_COOKIE_EXPIRES * 24 * 60 * 60 * 1000
),
httpOnly: true
}
res.cookie('jwt', token, cookieOptions);
res.status(200).redirect('/donor/dashboard');
}
})
} catch (error) {
console.log(error);
}
}
When I restart the app from Railway dashboard the app again works fine. I tried to use async/await but I think it is not helping and the callback function is not executed.