I am trying to take the country that the user inputs and check my database for said country, and find its corresponding country code. I will then take that country code and insert it into a different table. However i’m not sure where i’m going wrong in this particular line of code.
Here is the entire section of code that I am working with:
app.post('/add', async (req, res) => {
let userInput = [];
userInput.push(req.body.country);
console.log(userInput);
userInput.forEach((country) => {
const result = db.query(`SELECT country_code FROM countries WHERE country_name = ${userInput}`, function (err, result) {
db.query('INSERT INTO visited_countries (country_code) VALUES ($1)', [data.result] );
});
console.log(result);
if (userInput) {
const countries = db.query('SELECT country_code FROM visited_countries');
result.row.forEach((countries) => {
countries.push(country.country_code);
console.log(result.rows);
});
res.render('index.ejs', {countries: countries, total: countries.length});
}
});
});
This is the part I am trying to troubleshoot:
const result = db.query(`SELECT country_code FROM countries WHERE country_name = ${userInput}`
console.log(result) is returning undefined
Averie M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.