0

First of all, I'm new with node and express, so I think I'm losing something in the logic of express.js.

I'm trying to show the response from an API in frontend, but I can't even get it right in the backend.

routes/index.js

exports = module.exports = function (app) {
    // Views
    app.get('/', routes.views.index);
};

views/index.js

binance.prices((error, ticket) => {
    res.locals.ticket = ticket;
});
console.log(res.locals.ticket);

The last line is printing undefined and I don't understand why. Is because is out scope? or is the callback? I think is an asynchronicity problem, but I don't know how to solve it.

Any help will be appreciated.

exsnake
  • 1,767
  • 2
  • 23
  • 44
  • i think this problem is related to https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – brk Mar 18 '19 at 17:42
  • @brk Yes, I just saw that post but I didn't get how to apply it to my problem. – exsnake Mar 18 '19 at 17:47
  • basic thing, first execute the promise/ajax , then print the result only when the promise/ajax is resolved – brk Mar 18 '19 at 17:52
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – dNitro Mar 18 '19 at 21:31

0 Answers0