问题
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.
回答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;
})
});
回答2:
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
thenfunction to call your request
来源:https://stackoverflow.com/questions/33368250/undefined-is-not-a-valid-uri-or-options-object-in-expressjs