question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cote.js (promise) in Express

See original GitHub issue

Hi! I’m trying to use cote.js in my express app. Here my route code:

...
router.get('uuid', (req, res, next) => mainSvc.getUuid()
        .then( data => res.json(data) )
        .catch( next )
);

Here mainSvc code:

const      cote = require('cote'),
  uuidRequester = new cote.Requester({ name: 'uuid requester' });

module.exports = {
...
  getUuid:  ()=> {
       const  request = { type: 'uuid' };
        
       return  uuidRequester.send( request )
            .then( data  => {
                console.log('Data:', data);
                return  data;
            })
            .catch( err => {
                console.error('Error:',  err);
                return  Promise.reject( err );
            })
       ;
  }
...
};

Here my service to generate uuid:

const   uuid = require('uuid/v4'),
        cote = require('cote'),
   responder = new cote.Responder({ name: 'uuid responder' });

responder.on('uuid', (res, cb) => {
    cb({ uuid: uuid() });
});

And in console I got error:

uuid requester > service.online uuid responder#47cdd6ce-ccfb-4043-bcf7-e01b54b1b626 on 8000 Error: { uuid: ‘e917380b-8128-4bd7-b087-3ed136987289’ }

Error

What I did wrong and why I got Error instead Data? Thanks

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
dasherswcommented, Jun 12, 2017

@Keramet hello! Actually, it’s pretty simple. If you are using promises and callbacks together, you should make sure the first argument in the callback is the error object and it will be passed onto the catch handler, and the second argument is the actual result.

In your case, a simple cb(null, { uuid: uuid() }); would work. On the other hand, you can also use responder.on('uuid', (res) => Promise.resolve({ uuid: uuid() })).

1reaction
robophilcommented, Jun 12, 2017

@dashersw I think this should be highlighted more in the readme 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

dashersw/cote: A Node.js library for building zero ... - GitHub
a back-office with real-time updates for managing the catalogue of products and displaying sales with a RESTful API (express.js); a storefront for end-users ......
Read more >
cote JavaScript and Node.js code examples - Tabnine
Best JavaScript code snippets using cote(Showing top 15 results out of 315) ... 'pending' } deliveries.push(delivery) return Promise.resolve(delivery) }).
Read more >
Using Express.js Routes for Promise-based Error Handling
An Express.js route is a handler function corresponding to a given type of HTTP event matching a specified URI pattern. It's sent to...
Read more >
Force MicroService to await until RabbitMQ publishes a ...
I have a an API-GATEWAY MicroService that manages the entire show , and one of the services is called QueueService. Here is the...
Read more >
Using Promises with Express - Medium
The problem this function solves is that express doesn't natively handle promises, and so it doesn't support async / await . Route handlers...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found