I have a form capturing information from a registration, it is a dust layout with html form, part of a react app:
app.post('/add', function(req, res) {
// PG Connect
const { Pool, Client } = require('pg')
const connectionString = 'postgres://wol_admin:lolipop8@localhost/wol_master'
const pool = new Pool({
connectionString,
})
pool.query('SELECT * from customers');
pool.query('INSERT INTO customers (childfirstname, childmiddlename, childlastname, childgender, childage, childbirthdate, childgrade) VALUES ($1, $2, $3, $4, $5, $6, $7)',[req.body.childfirstname, req.body.childmiddlename, req.body.childlastname, req.body.childgender, req.body.childage, req.body.childbirthdate, req.body.childage], (err, result) => {
if(err) {
return console.error('error running query', err);
}
console.log('added to table', result);
res.redirect('/');
pool.end()
});
});
Is there a way I can pass the variables into the query as an object or some other type?
You can collect all the variables under an Object but it wont much change the stack.
First of all you better put your imports into the top.
You shouldn't pass your parameters directly from the object and it behaves like a dynamic query. It will be vulnerable to SQL Injection and many attacks. You can collect all the variables that can be used in a query and you can use Spread Sytax (...). You need to validate the inputs.
I would prefer to format my code as down below for security.