2017-06-10 14 views
0

私はノード、エクスプレス、パスポートとMySQLを使用しています 2-3テーブルのいくつかのフィールドの値を増分する関数を作成しました。 この関数は、postメソッドを使用して要求を送信するボタンを使用して呼び出されます。app.postはnode.jsで複数回実行できません

Cannot POST /nextad

私が行方不明です何:

問題は、それが、私はそれがこのエラーを送信ボタンを押して二回目の細かい1時間に働くということですか?

ここに私の.ejsファイルされる:

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

ここでは、ポストの要求に対応するapp.postである:ここでは

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

はpointsDistribute機能である:

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. - なぜこの問題が発生するのかを理解するために、私の機能の本体を読む必要があるとは思えません。

答えて

0

逆に、問題を理解するために関数の本体を読むことが重要だと思います。結局のところ、これはあなたがPOSTリクエストをしたときに呼び出される関数です。

ここで問題は、機能の最後にnext()を使用していると思います。

実際にnext()は、thisで非常によく説明されているように、ルートに一致する次のミドルウェア機能に渡ります。しかし、他のミドルウェアでリクエストを終了しないため(またはこのルートと一致する他のミドルウェアがないため)、再度クエリを実行することができず、エラーが発生します。

基本的に、あなたのようなものに変更することができます。また、あなたはあなたの関数で可能性のあるエラーを処理するの世話をする必要があり、それはexempleため、この場合にはnextを使用することは理にかなって

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

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() 
}) 
+0

ああ!だから、問題は、私がpointsDistribute関数の後に呼び出す関数がないということでした。私は私の次を変える();あなたのres.json()とそれは正しく動作するはずですか? –

+0

ありがとう、それは働いた!^_ ^ –

関連する問題