0

In the below code, if I remove event emitter part the function is working properly (only the commented code). With event emitter what Im doing wrong here? Im getting Error TypeError: Cannot set property 'undefined' of undefined

function mapDetail(username, password, oauthToken, callback) {
    var eventEmitter = new events.EventEmitter();
    eventEmitter.on('completed', function (message) {
        if (message != "false"); {
        /* var key;
        this.users[key] = {username: username, password: password}
        for (key in this.users) {
            if (this.users[key] && this.users[key].username == username && this.users[key].password == password) {
                // Update the oauthToken document to signal that key is authenticated
                for (var otherKey in this.oauth_users_request_tokens) {
                    if (this.oauth_users_request_tokens[otherKey] && this.oauth_users_request_tokens[otherKey].token == oauthToken) {
                        this.oauth_users_request_tokens[otherKey].authenticated = true;
                        callback(null, this.oauth_users_request_tokens[otherKey]);
                        return;
                    }
                }
            }
        } */
        }
        else
        callback(new Error("Authentication of user/password failed"), null);
    });
    LDAPObj.userDetails(username, password, eventEmitter);
}


LDAPObj.userDetails is the code from below link Node JS LDAP Auth User
In this code Im emitting the event in below scenarios On error eventEmitter.emit('completed',"false"); On success eventEmitter.emit('completed',"true");

Community
  • 1
  • 1
  • 1
    In the event callback, 'this' refers to the event emitter; you lose the context, hence the undefined error. – Paul Mougel Jan 30 '14 at 12:38
  • Thanks alot Paul Mougel. For the first time when I put wrong credentials Im getting unauthenticated. But when I pass correct credentials Im getting error [Error: write after end] – user3253071 Jan 30 '14 at 12:47
  • You didn't give us enough code then, what are you writing to write? – Paul Mougel Jan 31 '14 at 08:23
  • Possible duplicate of [javascript closure in loop](https://stackoverflow.com/questions/25352702/javascript-closure-in-loop) – Paul Sweatte Jun 15 '17 at 20:06

0 Answers0