0

I'm using node, express, passport with MySQL I made a function which increments the value of a few fields in 2-3 tables. The function is invoked using a button which sends the request using post method.

The problem is that it works fine 1 time then the second time I press the button it sends this error:

Cannot POST /nextad

What am I missing?

Here is my .ejs file:

<!DOCTYPE html>
<html>
<head>
 <title>viewads</title>
</head>
<body>
 <a href="/">Home Page</a>
    <a href="/logout">Logout</a>
 <form action="/nextad" method="post">
  <input type="submit" name="nextad" value="Next Ad">
 </form>
</body>
</html>

Here is the app.post corresponding to the post request:

app.post('/nextad',pointsDistrubute);

Here is the pointsDistribute function:

function pointsDistrubute(req,res,next){
    var id = req.user.UserId;
    console.log('id is:'+ id);
    var points = req.user.UserPoints;
    points = points + 1;
    //adding points to user.
    console.log('Updated user points are:'+points);
    console.log("update user set UserPoints='"+points+"' where UserId = "+id)
    connection.query("update user set UserPoints='"+points+"' where UserId = "+id);

    //adding points to cause starts vv
    console.log(" select * from DonateTo where UserId = "+id);
    connection.query(" select * from DonateTo where UserId = "+id ,function(err,rows){
        // DonateTo Rows = rows
        console.log("Rows:"+rows);            
        var keys = Object.keys(rows);
        console.log("keys:"+keys);
        //extracting object from RowDataPacket rows and storing in row
        for (var i=0; i<1; i++) { 
            var row = rows[keys[i]] ;
            console.log(row);
            var key = Object.keys(row);
            console.log("key:"+ key);
                //extracting id and causeId[1-5] from Object row and assigning keys 'key' to them 
                for (var j=row[key[6]]; j<key.length;) { 
                    console.log("row[key[j]]:");
                    console.log(row[key[j]]);
                    //row[key[j]] gives value of Pointer
                    var cid = row[key[row[key[j]]]];
                    // cid is the cause id the pointer points to.
                    if(row[key[j]]!=null){
                        console.log("update Causes set CausePoints= CausePoints + 1 where CauseId = "+cid);
                        connection.query("update Causes set CausePoints= CausePoints + 1 where CauseId = "+cid);
                        //adding points to the selected cause

                        j++;
                        j=j%6;
                        if (j==0) j++;
                        rows[0].Pointer = j;
                        console.log("update DonateTo set Pointer = "+j+" where UserId = "+id);
                        connection.query("update DonateTo set Pointer = "+j+" where UserId = "+id);
                        // value of j will move from current pointer and keep incrementing. 

                        break;
                    }
                    // value of j will move from current pointer and keep incrementing.    
                    j++;
                    j=j%6;
                    if (j==0) j++;
                    rows[0].Pointer = j;
                    console.log("update DonateTo set Pointer = "+j+" where UserId = "+id);
                    connection.query("update DonateTo set Pointer = "+j+" where UserId = "+id);
                }
        }
    });
    next();
 }

P.S. - I don't really think you need to read the body of my function to understand why this problem arises.

1 Answers1

0

On the contrary I think it is important to read the body of your function to understand the problem. After all, this is the function which is called when you make your POST request.

I think the problem here is you are using next() at the end of your function.

In fact next() will pass to the next middleware function that is matching the route, as it is very well explained in this other answer. But since you do not end your request in that other middleware (or because there is no other middleware that match this route at all), you are not able to query it again and that's why you get the error.

Basically you can change it to something like:

res.json({
  status: 200,
  message: "Got a succesfull POST request and I can do another if I'd like"
})

Also you should take care of handling possible error in your function, and it makes sense to use next in this case, for exemple:

var query = "SELECT * FROM DonateTo WHERE UserId = " + id
connection.query(query, function (err, rows) {
  if (err) return next(err)
  // here do your stuff
  res.end()
})
TGrif
  • 5,725
  • 9
  • 31
  • 52
  • Oh! So, the problem was that there was no function I was calling after the pointsDistribute function. I change my next(); with your res.json() and it should work right? – Ishaan Shakunt Jun 11 '17 at 04:41