process.nextTick error in node.js? - node.js

I am getting process.nextTick error on this very basic example of node.js.
Can someone please figure out?
Is node not able to start listening on port 8000?
# cat nodejs.js
net = require("net");
s = net.createServer();
net.on('connection', function (c) {
c.end('hello');
});
s.listen(8000);
# node nodejs.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Object #<Object> has no method 'on'
at Object.<anonymous> (/home/ec2-user/praveen/nodejs.js:4:5)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Array.0 (module.js:470:10)
at EventEmitter._tickCallback (node.js:192:40)

It's a typo in Ryan's slide! :-0
s/net.on/s.on/

It seems you're trying to capture an event on the library (net), but you should be looking at the connectionListener argument to createServer. Try this instead:
var net = require("net");
var server = net.createServer(function (c) {
c.end('Hello!'); // Implicitly fired on 'connection'
});
server.listen(8000);

For anyone else who might stumble here looking for why node pukes this error when they try to issue brunch watch --server, check and make sure you don't have any other servers running using the same port (i.e. in another shell).

Related

Unable to run 'npm start' in Mac

I am trying to run the following command "npm start" however it throws the following error:
internal/net.js:17
throw new RangeError('"port" argument must be >= 0 and < 65536');
^
RangeError: "port" argument must be >= 0 and < 65536
at assertPort (internal/net.js:17:11)
at Server.listen (net.js:1389:5)
at EventEmitter.listen (/Users/keyurshah/fulljs/node_modules/express/lib/application.js:617:24)
at Object.<anonymous> (/Users/keyurshah/fulljs/server.js:10:8)
at Module._compile (module.js:570:32)
at loader (/Users/keyurshah/fulljs/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/keyurshah/fulljs/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
[nodemon] app crashed - waiting for file changes before starting...
Check server.js line 10. Your port is probably specified there.
If not just declare it to be what you want right before the listen function.
From
app.listen(port, ...)
to
app.listen(8080, ...)
Otherwise you haven't shown any code not sure how we could help you with such limited information...
I recommend you start with more simple projects and get a feeling for how things work: http://expressjs.com/en/starter/hello-world.html
try to do this or change with any port number you like .listen(portNumber)
var http = require('http');
http.createServer( function (request, response) {
//your code
}).listen(8081);

Unhandled rejection TypeError when trying to promisify a node-mysql connection and query

I'm attempting to use bluebird's promisify with the node-mysql package. Node version is 4.2.4
var Promise = require('bluebird');
var mysqlClient = Promise.promisifyAll(require("mysql/lib/Connection").prototype);
Promise.promisifyAll(require("mysql/lib/Pool").prototype);
var connectionOptions = ({
host: 'localhost',
user: 'root',
password: '',
database: 'my_db'
});
var firstPost = "some sql here";
var results = [];
mysqlClient.connectAsync(connectionOptions).then(function(connection){
connection.query(firstPost, function(){
console.log('i reached line 26 of a node script. a minor miracle')
});
}).catch(function(err) {
console.log(err);
});
I get the error
[TypeError: Cannot read property 'socketPath' of undefined]
Stack trace:
Unhandled rejection TypeError: Cannot read property 'socketPath' of undefined
at Connection.connect (/vagrant/spam_smasher/node_modules/mysql/lib/Connection.js:87:32)
at Connection.tryCatcher (/vagrant/spam_smasher/node_modules/bluebird/js/release/util.js:11:23)
at Connection.ret [as connectAsync] (eval at <anonymous> (/vagrant/spam_smasher/node_modules/bluebird/js/release/promisify.js:184:12), <anonymous>:13:39)
at Object.<anonymous> (/vagrant/spam_smasher/bluebird.js:24:13)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:136:18)
at node.js:963:3
Why am I getting this error and/or how could I debug it further? I tried to remote debug it but am not sure what I'm looking at when I get a few steps down the prototype chain. The Async functions do appear to be attached to the MysqlClient object.
Remore debugging reveals that the following section in node-mysql's Connection.js file is throwing the error :
if (!this._connectCalled) {
this._connectCalled = true;
// Connect either via a UNIX domain socket or a TCP socket.
this._socket = (this.config.socketPath)
? Net.createConnection(this.config.socketPath)
: Net.createConnection(this.config.port, this.config.host);
socketpath is not something you need to define when using node-mysql directly
It seems the Bluebird docs are out of date. They miss that you also need ConnectionConfig to be available and so this was missing in my code, causing the error.
I chose to abandon this problem to preserve my sanity. There is further discussion here and you may also want to check out mysql-promise

Excepetion in data event handler binding on read stream

I am getting Error while attaching 'data' event handler on read stream.
If I place this handler at other place as mention in the code (before pipe), I don't see any exception.
Please help me, what is wrong here?
var Readable = require("stream").Readable;
var readStream = new Readable;
readStream.on('data',function(chunk){console.log(chunk);}); //Error
readStream.push('first data');
readStream.push('second data');
readStream.push(null);
//if I place data event handler here, no Error is seen
readStream.pipe(process.stdout);
//getting error while binding data event handler
events.js:72
throw er; // Unhandled 'error' event
^
Error: not implemented
at Readable._read (_stream_readable.js:446:22)
at Readable.read (_stream_readable.js:320:10)
at Readable.<anonymous> (_stream_readable.js:745:45)
at Readable.EventEmitter.emit (events.js:92:17)
at emitDataEvents (_stream_readable.js:771:10)
at Readable.on (_stream_readable.js:692:5)
at Object.<anonymous> (/home/pk/node/readableStream.js:12:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
You can't simply instantiate Readable stream, as it has no valid _read method implemented. If you want just to push some data to it (without performing real IO), just set it to noop:
readStream._read = function () {};

Creating temporary view in couchdb using node-couchdb-api

I am trying to connect couchdb using node-couchdb-api at nodejs level as mentioned in the following link http://dominicbarnes.us/node-couchdb-api/.My couchdb version is 1.1.1 and nodejs version is 0.6.10.
For creating temporary view as mentioned in api http://dominicbarnes.us/node-couchdb-api/api/database/tempView.html I have written the following code.
var couchdb = require("couchdb-api");
var server = couchdb.srv(localhost, 5984, false, false);
var db = server.db("test");
var map = function (doc) {
emit(null, 1);
};
var reduce = "_sum";
var query = { include_docs: true };
db.tempView(map, reduce, query, function (err, response) {
console.log(response);
});
But i am facing the following problem.
C:\Program Files\nodejs\node_modules\couchdb-api>node server.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Cannot read property '0' of null
at C:\Program Files\nodejs\node_modules\couchdb-api\lib\util.js:24:39
at Array.map (native)
at Object.formatFunction (C:\Program Files\nodejs\node_modules\couchdb-api\lib\util.js:22:25)
at Object.tempView (C:\Program Files\nodejs\node_modules\couchdb-api\lib\database.js:285:28)
at Object.<anonymous> (C:\Program Files\nodejs\node_modules\couchdb-api\server.js:27:4)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Array.0 (module.js:479:10)
please suggest me to resolve the issue.
Thanks in advance.
sorry about the problem you were experiencing. I'm the creator of that module, and I've just pushed version 1.1.2 up to NPM which addresses your problem. (and includes a unit test to make sure it doesn't happen again)
Just update to the latest version via npm update couchdb-api and you should be set to go. Let me know if you have further issues.

limestone module in Nodejs throwing error with nodejs and sphinx

I have been trying to connect sphinx server with nodejs and limestone module. But it is throwing error as follows. Please help me on this.
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: ENOENT, No such file or directory
at doConnect (net.js:549:5)
at Socket.connect (net.js:709:5)
at Object.createConnection (net.js:265:5)
at Object.connect (/home/node/node_modules/limestone/limestone.js:129:23)
at Object.<anonymous> (/home/node/www/bmchat-new/sphinx-connect.js:4:15)
at Module._compile (module.js:411:26)
at Object..js (module.js:417:10)
at Module.load (module.js:343:31)
at Function._load (module.js:302:12)
at Array.<anonymous> (module.js:430:10)
Source code of sphinx-connect.js
var limestone = require("limestone").SphinxClient(),
sys = require("sys");
limestone.connect("192.168.2.168:9312", // port. 9312 is standard Sphinx port. also 'host:port' allowed
function(err) { // callback
if (err) {
sys.puts('Connection error: ' + err);
}
sys.puts('Connected, sending query');
limestone.query(
{'query':'test', maxmatches:1},
function(err, answer) {
limestone.disconnect();
sys.puts("Extended search for 'test' yielded " +
answer.match_count + " results: " +
JSON.stringify(answer));
});
});
The issue is that i am using old limestone.js which i have got from npm(v1.0.106). So the updated one, you can get from github
Issue found is, old limestone.js using server_conn = tcp.createConnection(port); instead of the below one server_conn = tcp.createConnection(port, host);

Resources