我刚从 Node 开始,我被困在管理“回调地狱”上。我已经成功地使用事件发射器来从模块中触发主 js 文件中的事件,但是我无法弄清楚如何使用作用域来从模块的回调中触发事件。此外,我在从模块的回调中调用原型函数时遇到问题。
具体来说,这里:
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");
}
});
});
完整代码见: http: //pastebin.com/Gw6kzugk
编辑 - 上面可能的答案正是您应该寻找的。以下是我最终在我的情况下所做的事情。谢谢大家!