Javascript Nodejs模块中的作用域问题
我刚从Node开始,我一直在管理回调地狱。我已经成功地与事件发射器一起工作,以便从模块的主js文件中触发事件,但我无法确定如何使用作用域从模块的回调中触发事件。此外,我在从模块中的回调调用原型函数时遇到问题 具体而言,这里:Javascript Nodejs模块中的作用域问题,javascript,node.js,express,eventemitter,Javascript,Node.js,Express,Eventemitter,我刚从Node开始,我一直在管理回调地狱。我已经成功地与事件发射器一起工作,以便从模块的主js文件中触发事件,但我无法确定如何使用作用域从模块的回调中触发事件。此外,我在从模块中的回调调用原型函数时遇到问题 具体而言,这里: rows.forEach(function(thisRow, index, array) { myDb.query("SELECT COUNT(a.studentID) as total, m.fName, m.lName, m.id " + "from
rows.forEach(function(thisRow, index, array) {
myDb.query("SELECT COUNT(a.studentID) as total, m.fName, m.lName, m.id " +
"from `absences` a join `members` m on a.studentID = m.id " +
"where a.aDate>=" + myDb.escape(thisRow['beginDate']) + " and " +
"a.aDate<=" + myDb.escape(thisRow['endDate']) + " and a.aDate<'" + today + "' and m.memGroup = " + myDb.escape(thisRow['orchName']) +
"GROUP BY a.studentID ORDER BY total DESC", function(error, row){
if(row.length > 0) {
retValues.push({"fullName": thisRow.fullName, "shortName": thisRow.shortName, "absences": row});
}
if (index === array.length - 1) {
//This call to this fails because, I believe, it is out of scope.
//How can I access this? OR how can I emit an event here that will
//trigger the listener in the index.js?
this._alertServer;
console.log(retValues);
console.log("Done");
}
});
});
完整的代码可在以下位置找到:
编辑-以上可能的答案正是您应该寻找的。下面是我在这种情况下最后做的事情。谢谢大家 如注释中所述,您不能在回调中使用此选项。您需要在回调之外捕获它,如下所示:
rows.forEach(function(thisRow, index, array) {
var self = this; // the critical line
myDb.query("SELECT COUNT(a.studentID) as total, m.fName, m.lName, m.id " +
"from `absences` a join `members` m on a.studentID = m.id " +
"where a.aDate>=" + myDb.escape(thisRow['beginDate']) + " and " +
"a.aDate<=" + myDb.escape(thisRow['endDate']) + " and a.aDate<'" + today + "' and m.memGroup = " + myDb.escape(thisRow['orchName']) +
"GROUP BY a.studentID ORDER BY total DESC", function(error, row){
if(row.length > 0) {
retValues.push({"fullName": thisRow.fullName, "shortName": thisRow.shortName, "absences": row});
}
if (index === array.length - 1) {
// Use self here, not this
self._alertServer;
console.log(retValues);
console.log("Done");
}
});
});
虽然这可能不是最优雅的方式来处理这种情况,但我最终所做的是在一个上下文函数中传递它,而我花了很短的时间来编程Android程序
_getAttendanceBySession = function(err, rows, retValue, context) {
/*
Gets attendance for each session given
err -> Errors returned from last mySQL query
rows -> JS Object of session list
retValue -> string being passed to webserver
context -> 'this'
*/
var tmpHTML;
tmpHTML = retValue;
myDb.getConnection(function(err, conn) {
rows.forEach(function(thisRow, index, array) {
conn.query("SELECT COUNT(a.studentID) as total, m.fName, m.lName, m.id from `absences` a join `members` m on a.studentID = m.id where a.aDate>=" + (myDb.escape(thisRow.beginDate)) + " and a.aDate<=" + (myDb.escape(thisRow.endDate)) + " and a.aDate<'" + today + "' and m.memGroup = " + (myDb.escape(thisRow.orchName)) + " GROUP BY a.studentID ORDER BY total DESC", function(error, row) {
if (row.length > 0) {
tmpHTML = tmpHTML + ("<h3 class='text-center'>" + thisRow.fullName + "</h3><div class='panel-group' id='" + thisRow.shortName + "'>");
row.forEach(function(studentRow, index2, array2) {
var tmpLabel;
if (studentRow.total === 1) {
tmpLabel = 'label-default';
} else if (studentRow.total === 2) {
tmpLabel = 'label-warning';
} else {
tmpLabel = 'label-danger';
}
tmpHTML = tmpHTML + ("<div class='panel panel-default'><div class='panel-heading'><a class='attendance-link panel-title' data-toggle='collapse' data-parent='#" + thisRow.shortName + "' href='#" + studentRow.id + "-details'><span class='label pull-left " + tmpLabel + "'>" + studentRow.total + "</span>" + studentRow.fName + " " + studentRow.lName + "</a></div><div class='panel-body collapse' id='" + studentRow.id + "-details'></div></div>");
if (index2 === array2.length - 1) {
tmpHTML = tmpHTML + "</div>";
}
});
}
if (index === array.length - 1) {
conn.release();
context.emit("send-page", tmpHTML);
}
});
});
});
};
您不能在回调中访问此项。您需要在回调之外捕获它,var self=this;然后使用self.\u alertServer;假设_alertServer实际上是一个函数。可能是