1
var request = require('request');

request("http://example.com/index.php?username=username&password=password, function (error, response, body) {



var n1 = body.search('<user>');
var n2 = body.search('</user>');
var final = body.slice(n1+6, n2);


//it is working perfectly here. 
final10 = final;


 });

//The problem is that i can not call the "body" or variable "Final10" outside the Scope.

var final10=request.body


Hi, I am Newbie in Node JS. I am trying to make a Experimental Bot. i am using "request" to send a "Get" to my website. there through "Php", it is getting saved in "Mysqli" database.

Everything is working fine and i am getting the result. but, as i had got the data required, i can not access it. How can i get access to the 'body' of 'Request' from outside the function?

Please refer above to the code

Is there any way, to call it outside of it? that i can manage my code more easily

Please Note: it is just a little snap of the code the real code. the real code got a lot of if/else, loops and other functions. Turing it into bracket under brackets will be little complicated.

Thankyou

  • 1
    This is just a typical asynchronous callback issue in node.js. Any code that uses the result of the asynchronous code must be either INSIDE the callback itself or in a function you call from that callback. `request()` is non-blocking, so the calback itself is called long AFTER the code follow it is. So, you're trying to use a variable before it's even been set. Move the code inside the callback. – jfriend00 Apr 22 '20 at 17:47
  • FYI, there are hundreds of similar questions here. A search for nodejs, asynchronous callback will probably turn up lots of similar issues. – jfriend00 Apr 22 '20 at 18:09
  • Thank you for your comment. It is more preferable, if i can get it where it is. it will just save me from a lot of work. But, if nothing works. i will surely follow your advise. Thank you again – Suprin Aziz Talpur Apr 22 '20 at 18:09
  • With a plain callback, there is no other choice - that's how it works. If you use an http request library that supports promises such as `got()`, you can use `await` and write more sequential looking code - you will have to switch libraries and learn a bit about proper use of promises in Javascript (it will be worth it). You may also want to know that the `request()` library has been deprecated and is in maintenance mode (no new features coming). I personally wouldn't use it in a new project. – jfriend00 Apr 22 '20 at 18:11
  • Yup, i just checked it in "NPM" today and i really agree with you. but, i am new into it, so it is really a lot simpler than other libraries. But, i think, i do not have option, i will look into new libraries or will try await. . – Suprin Aziz Talpur Apr 22 '20 at 18:15
  • `request()` is not simpler than other libraries that use promises. I find `got()` easier and simpler to use and I can use it with `async/await` which fixes the issue you're asking about too. This is just a matter of you learning something new that you will want to know anyway for development in node.js. – jfriend00 Apr 22 '20 at 18:16
  • I will check it out. Thankyou Jfriend00.. – Suprin Aziz Talpur Apr 22 '20 at 18:18

1 Answers1

1

So the request function is asynchronous, which means that you call request("http://...") and node js fires that function, and then skips to the next line without waiting for it to finish. So if you had:

request("http://example.com/index.php?username=username&password=password", function() {
   console.log("1");
});

console.log("2");

You would see 2 logged before 1. Which brings us to the function that is the second argument of request: the callback function. You are passing in a function for request to call, once it has finished making the api request to your url. So if we change the above example to:

request("http://example.com/index.php?username=username&password=password", function() {
   console.log("1");
   console.log("2");
});

You would see 1 logged before 2.

Request takes the function you pass in and injects the parameters: error, response and body which you can work with inside the callback function.

Because you are working with request through a callback function here, which gets called asynchronously, you can only use body within the callback function.

request("http://example.com/index.php?username=username&password=password", function(error, response, body) {
   // do something with the response body
   console.log("Logging the response body", body);
});

You need to access the body within the scope of the callback function.

adrum
  • 71
  • 2
  • Firstly,Thankyou for your Answer Yes, that is exactly the problem. Can we use any loops+ if/else statement to keep repeating the code until request is complete and then declare the variable?. I really apologize, if it sounds stupid, since i am newbie in Node js. – Suprin Aziz Talpur Apr 22 '20 at 18:06
  • What is your end goal? What do you plan to do with the variable final10 – adrum Apr 22 '20 at 20:20