我有一個承諾,我作出處理登錄到我的網站之一,然後它應該收集一些信息。NodeJS承諾永遠不會到達第三個'然後()「電話
我的代碼:
var Promise = require('promise');
function login(user, pass){
return new Promise(function (fulfill, reject){
var options = {
url: 'https://url.com/this',
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://google.com/page'
}
};
request(options, function(err,httpResponse,body){
fulfill(body);
});
}).then(function(html){
var parsed = parseForm(html);
parsed['post_user_name'] = user;
parsed['post_user_pass'] = pass;
return parsed;
}).then(function(cred){
var query = querystring.stringify(cred);
var options = {
url: 'https://url.com/next',
method: 'POST',
form: query,
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://google.com/this'
}
};
request(options, function(err,httpResponse,body){
return 'gonext';
});
}).then(function(success){
query = querystring.stringify(cred);
options = {
url: 'https://url.com/loggedin',
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://google.com/loggedin'
}
};
request(options, function(err,httpResponse,body){
return 'go again';
});
}).then(function(success){
console.log('here');
query = querystring.stringify(cred);
options = {
url: 'https://url.com/myaccount',
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://google.com/account'
}
};
request(options, function(err,httpResponse,body){
fs.writeFile('text.txt', body);
return parseAuths(body);
});
});
}
我的問題:承諾永遠不會到達第三then()
功能。
同樣,如果我在一個then()
中寫入所有請求調用的代碼,然後嘗試將我需要的數據傳遞給下一個,那麼它也不會到達那裏。
function login(user, pass){
return new Promise(function (fulfill, reject){
var options = {
url: 'https://url.com/this',
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://url.com/'
}
};
request(options, function(err,httpResponse,body){
fulfill(body);
});
}).then(function(html){
var parsed = parseForm(html);
parsed['post_user_name'] = user;
parsed['post_user_pass'] = pass;
return parsed;
}).then(function(cred){
var query = querystring.stringify(cred);
var options = {
url: 'https://url.com/that',
method: 'POST',
form: query,
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://url.com/this'
}
};
request(options, function(err,httpResponse,body){
query = querystring.stringify(cred);
options = {
url: 'https://url.com/another',
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://url.com/that'
}
};
request(options, function(err,httpResponse,body){
query = querystring.stringify(cred);
options = {
url: 'https://url.com/account',
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://url.com/another'
}
};
request(options, function(err,httpResponse,body){
fs.writeFile('text.txt', body);
return parseAuths(body);
});
});
});
}).then(function(data){
console.log(data); // Never reaches this
});
}
我的問題:我在做什麼錯招搖?有什麼我失蹤?
parseForm是什麼樣子的? – Shaun
這是在第一個'then()'中,它使它通過 – Derek
你應該使用[request-promise](https://npmjs.com/package/request-promise)這樣你可以使用promise來鏈接你的請求。此外,當他們都在相同的範圍內時,您可以繼續重新分配選項。 – peteb