0

Here's my code containing PostgreSql query using pg-promise:

var db    = require('/pg-connection'),
    PQ    = require('pg-promise').ParameterizedQuery;

var fullname = 'username',
    email    = 'UniqueEmailHere',
    password = 'userPassword',
    uuid     = '15-20charactersRandomString';

var query = new PQ("INSERT INTO tablename.user (fullname, email, password, uuid, status) 
            VALUES($1, $2, $3, $4, $5) RETURNING id", 
            [fullname, email, password, uuid, '1' ] );

db.any(query)
    .then(function (data) {
        callback(null, data)
    })
    .catch(function (err) {
        console.log(err)
        callback(err)
    });

Query inserts the values (as given) into the database but at the same returns the error in catch:

[ReferenceError: uuid not defined]

I tried running the same query in pgAdmin4 and its running fine there as well. Can somebody please point out what's the real issue?

Murtuza Z
  • 5,639
  • 1
  • 28
  • 52
Adil
  • 21,278
  • 7
  • 27
  • 54
  • What is `PG`? What is `query`? You are asking about an error about an undeclared variable, and at the same time fail to include the most important parts in your question. – vitaly-t Jan 17 '17 at 09:48
  • 1
    http://stackoverflow.com/questions/12505158/generating-a-uuid-in-postgres-for-insert-statement – Shubham Batra Jan 17 '17 at 09:54
  • @vitaly-t thanks for pointing out, added these missing pre-requisites you mentioned. – Adil Jan 17 '17 at 10:09
  • I've tried to run it locally, and it goes as far as the connection without any problem, which tells me the issue is outside of the `pg-promise` library. Perhaps something specific to the use of `uuid`, I'm guessing. You can try and replace `uuid` in your insert with `"uuid"`, i.e. double-quote it, see if that makes any difference ;) – vitaly-t Jan 17 '17 at 10:25
  • Thanks for giving it a try, yeah I also tried running it at pgadmin and it runs flawlessly there. even this code snippet is perfectly inserting data into database but still its generating error in catch. can't say right now where the actual issue lies. – Adil Jan 17 '17 at 10:28
  • 1
    Try debugging it. It is possible the error is somewhere deep in the driver `node-postgres`. If so, then we should raise an issue against it. – vitaly-t Jan 17 '17 at 10:45
  • Found anything yet? – vitaly-t Jan 21 '17 at 18:05
  • didn't get time to work on that module as I've some higher priority tasks. I'll give it a look in a while IA – Adil Jan 23 '17 at 03:21

0 Answers0