0

I've been messing around with this Wikipedia node module in bash and I'm having some trouble saving the response to a variable. I can use console.log(response) to see the complete response but I can't get it to stick to a variable.

I tried maybe looking at the type of response but it just returns undefined. Any ideas?

var wikipedia = require("node-wikipedia");

wikipedia.page.data("Clifford_Brown", { content: true }, function(response) {
    console.log(typeof response);
});

Ideally I'd like to assign the response which has an html object to a variable and then use cheerio to get pieces of the html object with jQuery selectors, but I believe I need to at least get it in to a variable first, right?

This is part of the response.

 { title: 'Clifford Brown',
  redirects: [],
  text: { '*': '<div class="hatnote">For the scrutineer for the Eurovision Song Contest, see <a href="/wiki/Clifford_Brown_(scrutineer)" title="Clifford Brown (scrutineer)">Clifford Brown (scrutineer)</a>.

Edit/Fix

I was able to get this to work based on @idbehold's comments. Everything needed to be done in the call back so instead of calling the variable after the request, I returned it in the callback like this, which gave me access to the variable outside of the function.

var wikipedia = require("node-wikipedia");
var data;
wikipedia.page.data("Clifford_Brown", { content: true }, function(response) {
data = response;

})

Keon
  • 1,741
  • 3
  • 17
  • 27
  • Are you saying `console.log(typeof response);` in your code returns `"undefined"`? Where/how/when are you trying to assign it to a variable? – Felix Kling Jun 22 '15 at 18:30
  • 1
    You say that `response` is undefined but then you say "this is part of the response..." how do you get the response? – Explosion Pills Jun 22 '15 at 18:37
  • The response is undefined when assigned to a variable, if I console.log the response I can see it. – Keon Jun 22 '15 at 18:40
  • 2
    Anything you want to do with the `response` has to be performed **inside** the wikipedia callback function. – idbehold Jun 22 '15 at 18:46
  • @idbehold that worked. – Keon Jun 22 '15 at 18:50
  • Guess what the top question for the [tag:javascript] tag is: http://stackoverflow.com/questions/tagged/javascript?sort=frequent&pageSize=30 . – Felix Kling Jun 22 '15 at 18:55
  • Also *"The response is undefined when assigned to a variable, if I console.log the response I can see it."* still doesn't answer the follow up questions. E.g. if you do `var foo = repsonse; console.log(foo);` **inside** the callback, it works perfectly fine. Always include the code that actually demonstrates the issue instead of having us **guess** what you are doing. – Felix Kling Jun 22 '15 at 18:57
  • 2
    I don't think you've quite grokked the problem yet; even if you set the global variable inside the callback, it might not be available when you try to access it because the callback is waiting for the query to finish. And `return data` does nothing because the callback's return value is discarded. – JJJ Jun 22 '15 at 19:07

1 Answers1

0

There are many, many questions of this sort which appear on StackOverflow, and they all stem from not understanding the asynchronous nature of AJAX. Your code does not block at the .data() call to wait for the server to respond. Any lines of code that run after that call will run immediately, whereas the code inside your callback function will runs at some point in the future, after the data is returned from the server. Anything you do in response to getting the data must happen in that callback.

var wikipedia = require("node-wikipedia");

wikipedia.page.data("Clifford_Brown", { content: true }, function(response) {
    console.log(typeof response);
    // do something with the response here
});
// any code here will run before the above callback is invoked.
// don't try to do anything with the response here.
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315