luciotato / waitfor

Sequential programming for node.js, end of callback hell / pyramid of doom
MIT License
531 stars 29 forks source link

No method 'status' #28

Closed blakemcbride closed 9 years ago

blakemcbride commented 9 years ago

Hi,

My app is node+express. node version 0.10.35. I did the install with:

npm install wait.for npm install fibers (do I need this?)

I did:

var handleGet = function (req, res) {
    var fromRoot = req.params.fromRoot;
    var search = req.params.search;
    var res = wait.for(esUtils.findDocs,'components', search);
    res.status(404).send('Not found');  // just for testing
};
router.get('/:fromRoot/:search', function(req, res) {
    wait.launchFiber(handleGet, req, res); //handle in a fiber, keep node spinning
});

I got:

/home/blake/ComponentServer/node_modules/wait.for/waitfor.js:38
                            fiber.run();   
                                  ^
TypeError: Object #<Object> has no method 'status'
    at handleGet (/home/blake/ComponentServer/routes/components/search.js:25:9)
    at wait.launchFiber (/home/blake/ComponentServer/node_modules/wait.for/waitfor.js:15:31)

Process finished with exit code 8
blakemcbride commented 9 years ago

I upgraded node to 0.12.0. Same error.

blakemcbride commented 9 years ago

I am gathering that I need to run the server in a fiber too in order for this to work, however, you have an example of starting a node server in a fiber but not one for express. How can I start the express server in a fiber (if that is my problem)?

Thanks.

luciotato commented 9 years ago

yes you need fibers.

check the error: Object #<Object> has no method 'status'

fiber.run is the last exit point. but the error origin is:

at handleGet (/home/blake/ComponentServer/routes/components/search.js:25:9)

what's there? maybe this? res.status(404).send('Not found'); // just for testing

do res have a status member?

luciotato commented 9 years ago

also check in your code: var res = wait.for(esUtils.findDocs,'components', search); you must use wait.forMethod in order to bind this to the method owner (esUtils): var res = wait.forMethod(esUtils,'findDocs','components', search);

blakemcbride commented 9 years ago

Thanks. Turns out the problem was me creating a variable with the same name as the function parameter. You have to love JS.

When you do the npm install, does it already know that fibers is a dependency?

Thanks for a really great utility! I'll pass the word on.

luciotato commented 9 years ago

You have to love JS.

+1 to that

If your project is medium or large, I'll recommend you to use TypeScript or if you feel beta-adventurous my own LiteScript

The following normal typos in js will create subtle, hard to catch bugs:

Those three "beauties" make pure-javascript project non-scalable for more than a few lines to make a page dynamic (js original design goal)

If you are planning on a medium to large app, switch now to any type-checked lang generating js. Do not stay in pure-js for medium-size projects, it's a nightmare.