0

I'm doing an app with express in node. I thought about different DBMS before coming up with the idea of using PostgreSQL (I haven't got any NoSQL experience) but it's bugging me out due to some unexpected errors.

I'm trying to check if a user exist when he logs, but the Select statement doesn't work, don't know why:

var query = client.query('SELECT "name" FROM "User" WHERE "pass" = $1',  [req.body.password]);
console.log(query.toString());
query.on('row', function(row) {
    console.log('user "%s"', row.name);
});

After this the initial page should render (that actually works). I'm using the node-postgres driver, by the way.

If I put something like client.query('SELECT * FROM "User"'); the database works perfectly (and I don't need that behaviour either). I've read about the PostgreSQL identifier problems, but nevertheless it keeps happening.

jontxu
  • 49
  • 2
  • 6
  • You should put the variable in single quotes. – Jakub Kania Mar 07 '13 at 21:51
  • @Jakub, putting in single quotes `var query = client.query("SELECT 'name' FROM 'User' WHERE 'pass' = $1", [req.body.password]);` gives me a syntax error in 'User', don't know why. @mu, the docs of the [driver](https://github.com/brianc/node-postgres/wiki/Prepared-Statements) state how prepared statements (excuse the repetition) work. – jontxu Mar 07 '13 at 21:56
  • name tables must be in double quotes, strings in single quotes. So you should escape the quote. You could also do double dollar quote (http://sqlfiddle.com/#!1/6ccc5/1108). – Jakub Kania Mar 07 '13 at 22:03
  • If your identifiers are all legal, lower-case names, you don't need *any* quotes around them. Double quotes are only required for otherwise illegal (or not all lower-case) names. I don't see the error message you get in your question. Where is it? – Erwin Brandstetter Mar 07 '13 at 22:05
  • The error is `error: relation "User" does not exist`. It does exist, though. – jontxu Mar 08 '13 at 20:58

1 Answers1

0

Okay, turns out user table exists, and the table I created is case sensitive. It works know.

I tried select * from user on psql and it returned the current user. Tried select * from "User" and turned out I was wrong. Funny thing.

jontxu
  • 49
  • 2
  • 6