Nodejs模块中的作用域问题

时间:2014-03-17 17:16:54

标签: javascript node.js express eventemitter

我刚刚开始使用Node,而我却陷入了管理"回调地狱的困境。"我已成功设法使用Event发射器,以便从模块中触发主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

编辑 - 以上可能的答案正是您应该寻找的。以下是我在我的情况下最终做的事情。谢谢大家!

2 个答案:

答案 0 :(得分:2)

如评论中所述,您无法在回调中使用this。你需要在回调之外捕获它,如下所示:

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

答案 1 :(得分:0)

虽然它可能不是最优雅的想要接近这种情况,但我最终做的是在上下文函数中传递这个,这是我编程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);
        }
      });
    });
  });
};