0

I just started with nodes and express and I need a little help.

I am trying to get data from an api server (http://api.randomuser.me) and use this data on my view.

I have a api.js file: `var Client = require('node-rest-client').Client; var client = new Client();

client.get("http://api.randomuser.me/", function (data, response){
data = JSON.stringify(data.results);
console.log(data);
});"`

I need to use this variable data in a view ejs. But I just need to know how a set this result in another variable outside the function.

Thanks so much

  • Probably the most asked duplicate related to node.js. The short answer is that you consume that data INSIDE the callback. You don't assign it out and try to use it there because the ONLY place you know the timing of when that async callback was called and when the data is valid is inside the callback itself. – jfriend00 Jul 12 '16 at 02:52
  • If you ask a new question and show the overall problem you're trying to solve (like showing where you're trying to use the data you retrieve), rather than asking for this type of tenuous solution, then you might get some help for a new question. – jfriend00 Jul 12 '16 at 02:54
  • Thanks for your answer. Before I put this question I read a lot in many place and I didn't get the answer. at least I didn't get. – Douglas Correa Jul 12 '16 at 03:36
  • I want to use this variable in a view (EJS) I have rendered it to the view but I got an undefined. I have two source of data one is mysql it's working I did the render correctly but I was not able to render api to the same page. – Douglas Correa Jul 12 '16 at 03:52
  • You will have to show the view rendering code in a new question. If you show the code that is trying to use the asynchronous results and you show enough context around it so we can see the flow of execution, we can probably help. But, this question has been closed now, so you should open a new question with that code in it and ask about the actual problem you're trying to solve, not about your potential solution. – jfriend00 Jul 12 '16 at 03:55

0 Answers0