J. Alan
J. Alan

Reputation: 35

Unable to post via express using bodyparser

I have a web app containing four routes out of which two are executing insert query in Postgresql and two are executing select query. The post in insert is working fine but post in select query is not working. Is this problem of the query or post i'm unable to understand. Printing the post var gives undefined. Please help.

ws.post('/prab',function(req,res){
  var vill  =  req.body.addr;
  console.log(vill);
  if(!(vill = null) )
  {
    pg.connect(conString,function(err,client,done){
      if(err){
        return console.error('Could not connect to postgres' , err);
      }
      var results = [];
      var query = client.query("SELECT * FROM \"Area_info\" WHERE \"FOLDER_NAME = $1",[vill],function(err,result){
        console.log(vill);
        console.log(query);
        query.on('row', function(row) {
          results.push(row);
        });
        query.on('end', function() {
          done();
          return res.json(results);
        });
        if(err) {
          return console.error('error running query', err);
        }
        else{
          if(results.length == 0)
          {
            res.render('abs');
          }
          else{
            res.render('re');
          }
        }
      })
    })
  }
});

Upvotes: 0

Views: 72

Answers (2)

J. Alan
J. Alan

Reputation: 35

I solved my problem. It was silly on my part as i picked bootstrap template , i forgot to change the type of button from button to input. Thanks everybody to help me and waste your precious time on my silly mistake. I would be careful from next time.

Upvotes: 1

Quy
Quy

Reputation: 1373

This might be your problem:

if(!(vill = null) )

You're doing an assignment to vill and not the equality operator (== or ===). The assignment operator according to the Node REPL returns whatever is on the right hand side which in this case is null. Therefore, the statement (!(vill=null))above will always evaluate to true.

Upvotes: 0

Related Questions