Thread: error: there is no parameter $1

error: there is no parameter $1

From
Kinnard Hockenhull
Date:
I get "error: there is no parameter $1" when I try to run this code using the node-postgres client:

client = pg.connect(connectionString, function(err, client, done){
    if(err) console.log(err);
    client.query('INSERT INTO causes (cause_name, goal, organization, sponsor, submitter) VALUES ($1,$2,$3,$4,$5)', r, function(err){
      console.log('This is r' + r)
      if (err) console.log(err);
    });    
  });

Any advice?

PS:
This is the full error statement:
{ [error: there is no parameter $1]
  name: 'error',
  length: 87,
  severity: 'ERROR',
  code: '42P02',
  detail: undefined,
  hint: undefined,
  position: '81',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  file: 'parse_expr.c',
  line: '812',
  routine: 'transformParamRef' }

--
All the best,
Kinnard Hockenhull
Founder + CEO,

**********************************************************
Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues

Re: error: there is no parameter $1

From
David Johnston
Date:
Kinnard Hockenhull wrote
> I get "error: there is no parameter $1" when I try to run this code using
> the node-postgres <https://github.com/brianc/node-postgres> client:
>
> client = pg.connect(connectionString, function(err, client, done){
>     if(err) console.log(err);
>     client.query('INSERT INTO causes (cause_name, goal, organization,
> sponsor, submitter) VALUES ($1,$2,$3,$4,$5)', r, function(err){
>

This a node usage issue, not PostgreSQL.  That said you seem to be wanting
to use a prepared statement yet the syntax you show is a direct query.  In a
direct/literal query parameters (whether they be of the "?" or "$#" form)
are not allowed/understood.  Where in the code provided do you even attempt
to assign values to ${1-5}?

You either need to replace "${1-5}" with actual data or figure out how to
execute prepared queries in node.  The later is recommended since it is the
more secure way of doing things.

<goes looking at node-postgres>

Supposedly the "r" variable holds the array of five values you want to map
onto ${1-5}.  You do not show where "r" gets defined so maybe it is
undefined and thus you are not actually mapping values?

David J.



--
View this message in context:
http://postgresql.1045698.n5.nabble.com/error-there-is-no-parameter-1-tp5774552p5774559.html
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.


Re: error: there is no parameter $1

From
Kinnard Hockenhull
Date:



On Mon, Oct 14, 2013 at 9:47 PM, Kinnard Hockenhull <kinnard@bitbox.mx> wrote:
Broader code sample: 
app.post('/newcause', function (req,res){
  console.log(req.body);
  
  var g;
  
  var r = [];
  
  for (g in req.body)
  {
    r[g]=req.body[g];
    console.log('r[g] is ' + r[g]);
  }
  
  client = pg.connect(connectionString, function(err, client, done){
    if(err) console.log(err);
    client.query('INSERT INTO causes (cause_name, goal, organization, sponsor, submitter) VALUES ($1,$2,$3,$4,$5)', r, function(err){
      console.log('This is r' + r)
      if (err) console.log(err);
    });    
  });
});

Is this a scope issue? I tried passing r into pg.connect, that didn't work.


On Mon, Oct 14, 2013 at 4:51 PM, David Johnston <polobo@yahoo.com> wrote:
Kinnard Hockenhull wrote
> I get "error: there is no parameter $1" when I try to run this code using
> the node-postgres <https://github.com/brianc/node-postgres> client:
>
> client = pg.connect(connectionString, function(err, client, done){
>     if(err) console.log(err);
>     client.query('INSERT INTO causes (cause_name, goal, organization,
> sponsor, submitter) VALUES ($1,$2,$3,$4,$5)', r, function(err){
>

This a node usage issue, not PostgreSQL.  That said you seem to be wanting
to use a prepared statement yet the syntax you show is a direct query.  In a
direct/literal query parameters (whether they be of the "?" or "$#" form)
are not allowed/understood.  Where in the code provided do you even attempt
to assign values to ${1-5}?

You either need to replace "${1-5}" with actual data or figure out how to
execute prepared queries in node.  The later is recommended since it is the
more secure way of doing things.

<goes looking at node-postgres>

Supposedly the "r" variable holds the array of five values you want to map
onto ${1-5}.  You do not show where "r" gets defined so maybe it is
undefined and thus you are not actually mapping values?

David J.



--
View this message in context: http://postgresql.1045698.n5.nabble.com/error-there-is-no-parameter-1-tp5774552p5774559.html
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.


--
Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice



--
All the best,
Kinnard Hockenhull
Founder + CEO,

**********************************************************
Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues



--
All the best,
Kinnard Hockenhull
Founder + CEO,

**********************************************************
Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues

Re: error: there is no parameter $1

From
David Johnston
Date:
Kinnard Hockenhull wrote
>    var r = [];
>>
>>   for (g in req.body)
>>   {
>>     r[g]=req.body[g];
>>     console.log('r[g] is ' + r[g]);
>>   }
>>
>>   client = pg.connect(connectionString, function(err, client, done){
>>     if(err) console.log(err);
>>     client.query('INSERT INTO causes (cause_name, goal, organization,
>> sponsor, submitter) VALUES ($1,$2,$3,$4,$5)', r, function(err){
>>       console.log('This is r' + r)
>>       if (err) console.log(err);
>>     });
>>   });
>> });
>>
>> Is this a scope issue? I tried passing r into pg.connect, that didn't
>> work.
>>

Apparently not many Node.js users on these lists...

Since you are logging lots of stuff to the console you should provide that
information as well.  The client.query(...) code looks correct syntactically
but if "r" does not have exactly (at least?) 5 values in the array the query
execution will fail.  You have not shown us what is in "r" at the time
client.query(...) is executed.

David J.



--
View this message in context:
http://postgresql.1045698.n5.nabble.com/error-there-is-no-parameter-1-tp5774552p5774955.html
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.