【发布时间】:2017-06-10 04:59:50
【问题描述】:
我在 MySQL 中使用 node、express、passport 我做了一个函数,它增加了 2-3 个表中几个字段的值。 该函数使用一个按钮调用,该按钮使用 post 方法发送请求。
问题是它工作正常 1 次然后我第二次按下按钮它发送此错误:
无法发布 /nextad
我错过了什么?
这是我的 .ejs 文件:
<!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>
这里是post请求对应的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();
}
附: - 我真的不认为你需要阅读我的函数体来理解为什么会出现这个问题。
【问题讨论】:
标签: javascript mysql node.js ejs