Question

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

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);
    });    
  });
});

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' }
Was it helpful?

Solution

The error from executing your query means that r is not getting populated correctly. It's worth being explicit about user input so your system doesn't raise an error downstream (in the query) rather than at the problem root:

app.post('/newcause', function (req,res){
  console.log(req.body);

  var g;

  var r = [];

  r.push(req.body.causename, req.body.Goal, req.body.Organization, req.body.sponsor, req.body.submitterEmail);

  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.toString())
      if (err) console.log(err);
    });    
  });
});

It's also worth mentioning that it might be a good idea to standardize your variable name-casing in the post submission.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top