error: syntax error at end of input - Nodejs and PostgresSQL(Heroku)

2523 views javascript
3

When the user enters the website on the first time he gets a special string assigned to him. I am saving that string in a row that contains the user's IP as well. So when the user enters the website I want to query the DB and ask if it finds his IP in the table. I get a syntex error altough when I use the CLI it works find and returns t if the IP exists or f if it's not, as it should be.

The code:

let thisIP = req.headers['x-forwarded-for'] ||
    req.connection.remoteAddress ||
    req.socket.remoteAddress ||
    (req.connection.socket ? req.connection.socket.remoteAddress : null);

client.query("SELECT exists(SELECT 1 from registrations WHERE ip='" + thisIP + "'",
    (err, res) => {
        if (err) throw err;
        console.log(res.rows[0]);
    });

The error:

error: syntax error at end of input

answered question

You miss a closing parenthesis at the end of your query. Also, consider to use prepared statement.

1 Answer

2

You are missing the closing parenthesis of the exists function.

posted this

Have an answer?

JD

Please login first before posting an answer.