I just started playing around with node.js (and javascript) and may have a fundamental misunderstanding. The following code works as expected. It is taken from a tutorial for a simple API with express, except for the extracted function to get the data (but the problem also occurs with restify except for a less informative stack trace).
var express = require('express');
var app = express();
var getdataandsend = function(request,response) {
var res = {a:1,b:2}; // actually sql query using request.params
response.send(res);
}
app.get('/:id', function (request, response) {
getdataandsend(request,response);});
var server = app.listen(1234, function () {});
However, since I only pass response
to getandsenddata
in order to call its send
method, I figured that I might as well pass a reference/pointer to the method itself instead of the whole object:
var getdataandsend = function(request,respfun) {
respfun({a:1,b:2});
}
app.get('/:id', function (request, response) {
getdataandsend(request,response.send);});
But this results in the following
TypeError: Cannot read property 'req' of undefined
at send (...\test\node_modules\express\lib\response.js:103:17)
at getdataandsend (...\test\misctest.js:6:9)
at ...\test\misctest.js:9:6
at Layer.handle [as handle_request] (...\test\node_modules\express\lib\router\layer.js:95:5)
...
The relevant line in response.js is var req = this.req;
, which is the first occurence of a variable of that name in that file. Apparently, the value of this
is different between the two calls above, even though the response object's method send
is called from the same place (getdataandsend
). Following this answer and that answer the observed behavior might make sense if the first call is considered a method and the second a function call, but why would that be the case? send
is still a method of response
, isn't it? Or are both function calls (...then what is response
)?