I have db airport, there is TABLE tourists and in it COLUMN: id and also i have TABLE: flights, and there is column: listoftouristsbyid which is array of integers. I need to check if i passed to REST API request integer that is id of Tourist that not EXISTS. Means there is no tourist with given id (id is primary, autoincrementing key). I wrote something like that:
app.post('/flights', function(req, res) {
pool.connect(function(err,client,done) {
if(err) {
return console.log("Error fetching clients", err);
}
client.query("SELECT tourists.id FROM tourists WHERE tourists.id = " + req.body.listoftouristsbyid[i], function (err, result) {
done();
if (result.rows === 0) {
return console.log("Tourist "+req.body.listoftouristsbyid[i]+" you want to add does not exist" + err);
}
})
client.query('INSERT INTO flights(departuredate, arrivaldate, numberofseats, listoftouristsbyid, ticketprice) VALUES($1, $2, $3, $4, $5)',
[req.body.departuredate, req.body.arrivaldate, req.body.numberofseats, req.body.listoftouristsbyid, req.body.ticketprice]);
done();
res.redirect('/flights');
})
})
I know ITS A VALID QUERY, because when i type it in PSQL shell it returns
airport=#
SELECT tourists.id FROM tourists WHERE tourists.id = 15;
id
----
(0 rows)
But its not working as javascript code - means code should terminate with error, but condition is not fulfiled. How do i write condition in Javascript that catches when SELECT tourists.id FROM tourists WHERE tourists.id = 15 gives 0 rows?