node-restful
node-restful copied to clipboard
"Cannot call method 'toString' of undefined" when using routes
Following code caused the error in http.js - statusCode is undefined. http.js:1181 var statusLine = 'HTTP/1.1 ' + statusCode.toString() + ' ' +
It looks like the handler is never called.
Project.route("questions", {
handler: function(req, res, next, err, model) {
res.send(model.questions);
return next();
},
detail: true,
methods: ['get']
});
Project is working fine with all methods.
[email protected] node_modules/node-restful └── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])
Don't call next() if you already send the response
That's not the point. Sorry for giving a confusing example.
I put a breakpoint on the first line of those two (tried with different lines too), the problem happens before it's called. I debugged the code step by step and found that preprocess is creating res.locals and another callback registered after that is trying to run res.status(res.locals.status_code)
model.js:
function preprocess(req, res, next) {
req.body = req.body || {};
req.query = req.query || {};
req.quer = this.filter(req, this.find({}));
if (!('locals' in res)) {
res.locals = {};
}
//dirty hack that made it work, but send empty response: res.locals.status_code=200;
res.locals.bundle = {};
req.templatePath = resolveTemplate(req);
next();
}
handlers.js:
exports.last = function(req, res, next) {
if (res.locals.bundle) {
if (req.body.format === 'js') {
return res.send(res.locals.bundle);
} else if (req.body.format === 'html' || req.query.format === 'html') {
return res.render(this.templateRoot + '/' + req.templatePath, res.locals.bundle);
} else {
// v-- status_code was never created
return res.status(res.locals.status_code).json(res.locals.bundle);
}
}
res.send();
};
If I put the status_code where it's expected - the query returns empty object and still never calls the handler
Ok, found the root of the problem, but it's not related to the code being vulnerable to empty status here. Adding a new issue. This can remain as is - only about status_code being undefined
ok i found the probleme
Project.route("questions", { handler: function(req, res, next, err, model) { res.send(model.questions); return next(); }, detail: true, methods: ['get'] });
Is not good
Project.route("questions", { handler: function(req, res, next) { res.send(model.questions); return next(); }, detail: true, methods: ['get'] });
this work now
please MAJ your exemple