node-restful icon indicating copy to clipboard operation
node-restful copied to clipboard

"Cannot call method 'toString' of undefined" when using routes

Open naugtur opened this issue 12 years ago • 4 comments

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])

naugtur avatar Sep 25 '13 10:09 naugtur

Don't call next() if you already send the response

baugarten avatar Sep 25 '13 10:09 baugarten

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

naugtur avatar Sep 25 '13 11:09 naugtur

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

naugtur avatar Sep 25 '13 11:09 naugtur

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

jnath avatar Oct 05 '13 11:10 jnath