0

I have db airport, there TABLE tourists and COLUMN: id and TABLE flights, and there 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);
        }
        for (let i=0;i<req.body.listoftouristsbyid.length();i++) {
            if (client.query("EXISTS (SELECT * FROM tourists WHERE tourists.id = " + req.body.listoftouristsbyid[i]) == null)
                    return console.log("Tourist "+req.body.listoftouristsbyid[i]+" you want to add does not exist");
        }
        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');
    })
})

But its obviously not working, and even when i type this command into POSTGRESQL it gives me syntax error. So i need QUERY that gets evaluated, and when its false it should escape function and stop from passing numbers that are IDs of items that does not EXISTS. Please help


EDIT: I tried with legit SELECT query, still to no avail. Return does not break function and POST request adds new flight, even though it should not. Here is code"

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');
    })
})
λjk.jk
  • 127
  • 1
  • 12
  • I'm not an expert for postgressql but I am pretty sure your sql is wrong. Try running the sql directly. I dont think `exists` can be used as a statement in this context. – TomC Sep 27 '18 at 00:20
  • yes, i said it gives me SYNTAX ERROR in psql shell. I found in another stack thread, any alternatives to that? I also tried SELECT tourists.id FROM tourists WHERE tourists.id = 15; but it gives me id ---- (0 rows) , and i dont know how to use this output – λjk.jk Sep 27 '18 at 00:27
  • 1
    Ok, so that is two separate questions. There is no point putting up a js example where you *know* the sql is wrong. Question is really how to know the query returned zero rows. – TomC Sep 27 '18 at 00:29
  • https://stackoverflow.com/a/16467634/2301088 – Connor Nov 29 '18 at 17:48

0 Answers0