0

I am using expressjs, after running the webapagetest I am storing the url which is in string format in a variable then I am using this variable in request node module to get the data from that url, but I am getting undefined is not a valid uri or option. here is the my JavaScript code

 var WebPageTest = require('webpagetest');
    var wpt = new WebPageTest('url of private webpagetest instance server');

    wpt.runTest('https://google.com', function(err, data) {
        console.log(err || data);
        console.log(data.data.summaryCSV);
        sid = data.data.summaryCSV;
        console.log(typeof sid);

    });

    request(sid,function(error,response,body){
        console.log(body);

I am getting error at last line "request"

thanks.

Grokify
  • 15,092
  • 6
  • 60
  • 81
Siddharth Sinha
  • 578
  • 2
  • 15
  • 35

2 Answers2

1

Try for this:

var WebPageTest = require('webpagetest'); 
var wpt = new WebPageTest('server-url'); 
wpt.runTest('some url', function(err, data) {
    console.log(err || data); 
    console.log(data.data.summaryCSV); 
    console.log("GOING INTO REQUEST MODULE!!"); 
    request('data_url', function (error, response, body) { 
       if (error) { 
      console.log(error); 
      } 
      console.log(body); 
      var data = body;
    })
});
Subburaj
  • 5,114
  • 10
  • 44
  • 87
  • I did as you instructed but the point is my code is getting struck into an infinite loop but in the same program whenever i am manually writing the url on place of sid in request, the program is working all fine. Please suggest something – Siddharth Sinha Oct 28 '15 at 10:04
  • I printed sid it is giving me the correct result and it of type string, the point is, as control comes to request the whole program gets into infinite loop, I don't know what is exactly happening – Siddharth Sinha Oct 28 '15 at 10:46
  • here is the full code var WebPageTest = require('webpagetest'); var wpt = new WebPageTest('server-url'); var data_url; wpt.runTest('some url', function(err, data) {console.log(err || data); data_url=data.data.summaryCSV; console.log("GOING INTO REQUEST MODULE!!"); request('data_url', function (error, response, body) { if (error) { console.log(error); } console.log(body); var data = body; – Siddharth Sinha Oct 28 '15 at 11:08
  • i executed the code and at the terminal it gives me this: GOING INTO REQUEST MODULE!! [Error: Invalid URI "data_url"] undefined Please tell me what to do now, i am really stuck with my project . – Siddharth Sinha Oct 28 '15 at 12:02
  • WebPagetest is an open source project that is primarily being developed and supported by Google as part of their efforts to make the web faster. 'some url' is the name of the site for which i want to test the performance, say http://my-company-name.com, and webpagetest is the node module i installed from https://www.npmjs.com/package/webpagetest wpt.runTest tests the url provided, sends back 4 url which contains results in csv format, json format, xml format, and human readable format respectively, now i want to hit the url which contains data in csv format, parse those data and get charts. – Siddharth Sinha Oct 28 '15 at 12:21
  • Actually tests are getting repeated continiously. – Siddharth Sinha Oct 28 '15 at 12:38
  • Here tests means your testing?? – Subburaj Oct 28 '15 at 12:41
1

You are getting the error, because the runTest function is an asynchronous operation, so you should not depend on the values set inside in the following lines of code, because you cannot be sure, that the asynchonous call has already finished. To achieve what you would like to do, you can either:

  • call the code directly inside the callback function (as @Subburaj suggests),
  • call your own callback function, inside the callback (in this case, wrap the request in a function, and call it inside)
  • use a promise library like Bluebird, then you can just simple use the then function to call your request
meskobalazs
  • 15,741
  • 2
  • 40
  • 63