我正在尝试从数据库mysql中获得一些结果。我正在调用一种方法,该方法可以插入数据库,而另一种方法可以读取之前插入的内容。
我这样做是因为node.js无法同步工作。这是我的代码:
exports.list = function(req, res){
var moduleRows;
req.getConnection(function(err,connection){
var query = connection.query('SELECT * FROM questions WHERE module_id = ?',[X],function(err,rows)
{
for(var i = 0;i < rows.length;i++){
moduleRows = rows[i];
GetResults(moduleRows);
}
});
var GetResults = function(moduleRows) {
var RightAnswer = 0;
var WrongAnswer1 = 0;
var query = connection.query("SELECT * FROM studentsanswers WHERE Question = '"+moduleRows.question+"' ",function(err,rows2)
{
for(var ii = 0;ii < rows2.length;ii++){
if (rows2[ii].Answer == moduleRows.RightAnswer){
RightAnswer++;
}
else if (rows2[ii].Answer == moduleRows.WrongAnswer1){
WrongAnswer1++;
}
}
var Data = {
question: moduleRows.question,
RightAnswer: moduleRows.RightAnswer,
WrongAnswer1: moduleRows.WrongAnswer1,
NumberOfRightAnswer: RightAnswer,
NumberOfWrongAnswer1 : WrongAnswer1
};
insertResults(Data);
return;
});
}
var insertResults = function(Data) {
var query = connection.query("INSERT INTO statistics set ? ",Data, function(err, rows)
{
PrintResults();
return;
});
}
var PrintResults = function() {
var query = connection.query("SELECT * FROM statistics" ,function(err,rows)
{
res.render('StatisticsByModules',{page_title:"Questions - Node.js",data:rows});
return;
});
}
});
};
我遇到了错误: 错误:发送标头后无法设置标头。 TypeError:req.next不是函数
答案 0 :(得分:0)
在以下代码中,您多次调用<select name="userDdl" onChange={::this._onSelectedUserChange}>
{users.map(user => (
<option key={user.id} value={user.id}>{user.name}</option>
))}
</select>
,而几次调用GetResults(moduleRows)
,在您的PrintResults
方法中,您发送了PrintResults
几次,而这是您无法执行的。单个请求。首先,您必须制作数据列表,然后使用查询的数据调用res.render
。
req.render