gpt4 book ai didi

javascript - app.post 无法在 Node.js 上多次运行

转载 作者:行者123 更新时间:2023-11-29 18:48:30 26 4
gpt4 key购买 nike

我正在使用 Node、Express、Passport 和 MySQL我创建了一个函数,可以增加 2-3 个表中一些字段的值。使用按钮调用该函数,该按钮使用 post 方法发送请求。

问题是它工作正常一次,然后第二次按下按钮时它会发送此错误:

Cannot POST /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);

这是pointDistribute函数:

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

附注- 我真的认为您不需要阅读我的函数主体来理解为什么会出现这个问题。

最佳答案

相反,我认为阅读函数正文以理解问题很重要。毕竟,这是您发出 POST 请求时调用的函数。

我认为这里的问题是您在函数末尾使用了 next()

事实上 next() 将传递到与路由匹配的下一个中间件函数,正如 this 中对此进行了很好的解释。其他答案。但是,由于您没有在其他中间件中结束请求(或者因为根本没有其他中间件与此路由匹配),因此您无法再次查询它,这就是您收到错误的原因。

基本上你可以将其更改为:

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

此外,您还应该注意处理函数中可能出现的错误,在这种情况下使用 next 是有意义的,例如:

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

关于javascript - app.post 无法在 Node.js 上多次运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44469727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com