The context of my problem is that I'm trying to do a website signup page, and within that, I'm trying to check whether an email is already included in the database. To do this I'm trying to write a function which will return all the emails in the database so I can then do a
if (emails.includes(new_user_email) {
return error;
}
kind of thing.
Essentially I'd like something that can return a boolean telling me if the email already exists. So returns an array I can check, or even just searches for the email in the database, but that waits before returning true or false.
I've tried the function below to return an array of emails, although I understand this doesn't work due to the asynchronous nature of sqlite3. My issue is that I can't get a function that waits for the sql code to run before returning a result.
function get_all_emails() {
let emails = []
db.all("SELECT email FROM users", [], (err, rows) => {
if (err) {
throw err;
}
rows.forEach((row) => {
emails.push(row.email);
});
return emails;
});
}