node.js - NodeJS : can not emit event "Object ...{} has no method 'emit' -
i followed few nodejs tutorials ang got through simple rest webservice. webservices listens /api/accounts email , password, proceeds lookup in cassandra cluster. use eventemitter avoid callback hell. works fine until emit('success') attempted :
events.js:72 throw er; // unhandled 'error' event ^ typeerror: object select * accounts email_accounts=?,test@gmail.com,77ddcc8d868e1532f86f0e25f35d43e5,function _checkpassword(error, response) { var rows=response.rows; if (_checkforerrors(error, rows , 'password')) { return false; } else { if ( rows.length == 1 ) { console.log('got user profile password_accounts ' + rows[0].pwd_accounts); this.emit('success', rows[0] ); } } } has no method 'emit' @ array._checkpassword (/home/scoulibaly/node/login.js:33:10) @ /home/scoulibaly/node/node_modules/node-cassandra-cql/index.js:220:16 @ connection.handleresult (/home/scoulibaly/node/node_modules/node-cassandra-cql/lib/connection.js:308:3) @ resultemitter.eventemitter.emit (events.js:106:17) @ resultemitter.bufferandemit (/home/scoulibaly/node/node_modules/node-cassandra-cql/lib/streams.js:456:10) @ resultemitter.each (/home/scoulibaly/node/node_modules/node-cassandra-cql/lib/streams.js:440:17) @ resultemitter._write (/home/scoulibaly/node/node_modules/node-cassandra-cql/lib/streams.js:413:10) @ dowrite (_stream_writable.js:223:10) @ writeorbuffer (_stream_writable.js:213:5) @ resultemitter.writable.write (_stream_writable.js:180:11)
my main server.js. use express , router() setup routes :
// server.js // base setup // ============================================================================= // call packages need var express = require('express'); // call express var app = express(); // define our app using express var bodyparser = require('body-parser'); var login = require('./login'); // configure app use bodyparser() // let data post app.use(bodyparser.urlencoded({ extended: true })); app.use(bodyparser.json()); var port = process.env.port || 8080; // set our port // routes our api // ============================================================================= var router = express.router(); // instance of express router // middleware use requests router.use(function(req, res, next) { // logging console.log('something happening.'); next(); // make sure go next routes , don't stop here }); router.route('/accounts') // account id (accessed @ http://localhost:8080/api/accounts/:account_id) .get(function(req, res) { var login = new login(req.query.email, req.query.password); login.on('error', function (error) { res.writehead(500); res.end(); }); login.on('failure', function (reason) { if (reason == 'email') { res.end('wrong email!'); } else if (reason == 'password') { res.end('wrong password!'); } }); login.on('success', function (data) { res.json(data); }); login.perform(); }); // test route make sure working (accessed @ http://localhost:8080/api) router.get('/', function(req, res) { res.json({ message: 'hooray! welcome our api!' }); }); // more routes our api happen here // register our routes ------------------------------- // of our routes prefixed /api app.use('/api', router); // start server // ============================================================================= app.listen(port); console.log('magic happens on port ' + port);
my login.js extends eventemitter :
// login.js var util = require('util'); var eventemitter = require('events').eventemitter; var cql = require('node-cassandra-cql'); function login(email,password) { // error checking function _checkforerrors(error, rows, reason) { if (error) { console.log('emit err'); this.emit('error', error); return true; } if (rows.length < 1) { console.log('emit fail'); this.emit('failure', reason); return true; } return false; } // check {email,pwd} function _checkpassword(error, response) { var rows=response.rows; if (_checkforerrors(error, rows , 'password')) { return false; } else { if ( rows.length == 1 ) { console.log('got user profile password_accounts ' + rows[0].pwd_accounts); this.emit('success', rows[0] ); } } } function perform() { var client = new cql.client({hosts: ['localhost', 'localhost'], keyspace: 'node'}); client.execute('select * accounts email_accounts=?', [ email , password ], _checkpassword ); } this.perform = perform; } util.inherits(login, eventemitter); module.exports = login;
it looks 'this' in this.emit() lines refer cql object. in understanding, cql drivers calls callback whenever data ready, 'this' object not login object anymore. wrong ? how can have emit() work ?
thank you
near top of login object definition, create handle this
via:
var _this = this;
then use _this
instead of this
when calling emit
method. _checkpassword
said close on _this
(which can't on this
).
the this
keyword special snowflake dynamically bound when enclosing function invoked; if pass function refers this
off other functions, make sure this
think is. :)
Comments
Post a Comment