I am using the nodejs request module from here: https://github.com/mikeal/request -- There are cookies involved but not part of this code.
Excuse the non-optimized code, I've been trying various stuff trying to fix this problem with no success. I have this simple script:
function getPage(curpage)
{
if(curpage <= pages)
{
var newpage = curpage + 1;
console.log('getting page '+newpage );
request.get({ uri: 'http://someurl.com/test', qs: { p : newpage }}, function(error, response, body) {
if(error)
{
[...]
}
else
{
console.log(response.req.path);
}
getPage(newpage);
});
}
else
{
console.log("We're done!\n");
process.exit();
}
}
The problem here, is my response.req.path seems to get stuck at 2. I get:
getting page 1
/id/bradpitt5/inventoryhistory?p=1
getting page 2
/id/bradpitt5/inventoryhistory?p=2
getting page 3
/id/bradpitt5/inventoryhistory?p=2
getting page 4
/id/bradpitt5/inventoryhistory?p=2
As you can see, the "newpage" is used properly when getting the page, but the request path is set wrong? I can't make sense out of this. Can anyone figure out what I am doing wrong here?
I just start with getPage(0);
I also did this using a different method, just to make sure I wasn't crazy:
for( var i = 1; i <= pages; i++ ) {
urls.push( {'url' : 'http://somepage.com/test?p='+ i, 'done' : false } );
}
function getNextPage() {
for(var i = 0; i < pages; i++ ) {
if( urls[i].done == false ) {
break;
}
}
if( urls[i] && urls[i].done == false ) {
urls[i].done = true;
console.log( 'requesting: '+ urls[ i ].url );
request.get( urls[ i ].url, function(error, response, body) {
if(error) {
[...]
} else {
console.log( 'received: '+ response.req.path +' ok. ');
}
getNextPage();
}
);
} else {
console.log("we're done!");
process.exit();
}
}
This also gets stuck at page 2.
The implication is that:
{ uri: 'http://someurl.com/test', qs: { p : newpage }}
is not being updated each time request.get()
is called. I wonder if it is worth trying to create a new variable:
var options = {
uri: 'http://someurl.com/test',
qs: {
p: newpage
}
};
// let's debug just to be sure
console.log( " options = %s", JSON.stringify( options, null, 2 ) );
request.get( options, function(error, response, body) {
...
I don't know if this is the case but I wonder if the object created within the function call isn't evaluated just once.
It appears I only had "2 pages" of data, and the site I was requesting from redirected to the last page if I was trying to go higher than the number of pages. :(
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.