2016-05-12 47 views
-1

我有一個承諾,我作出處理登錄到我的網站之一,然後它應該收集一些信息。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 
    }); 
} 

我的問題:我在做什麼錯招搖?有什麼我失蹤?

+0

parseForm是什麼樣子的? – Shaun

+0

這是在第一個'then()'中,它使它通過 – Derek

+0

你應該使用[request-promise](https://npmjs.com/package/request-promise)這樣你可以使用promise來鏈接你的請求。此外,當他們都在相同的範圍內時,您可以繼續重新分配選項。 – peteb

回答

1

這種方法使用request-promise。您可以將所有請求作爲承諾並保持連鎖一致。如果您遇到錯誤,它會正確傳播到最近的catch()

var Promise = require('promise'); 
var rp = require('request-promise'); 


function login(user, pass) { 
    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' 
     } 
    }; 

    return rp(options) 
     .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 opts = { 
       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' 
       } 
      }; 
      return rp(opts); 
     }).then(function(success){ 
      var query = querystring.stringify(cred); 
      var opts = { 
       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' 
       } 
      }; 
      return rp(opts); 
     }).then(function(success){ 
      console.log('here'); 
      var query = querystring.stringify(cred); 
      var opts = { 
       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' 
       } 
      }; 

      return rp(opts); 
     }) 
     .then(function(body) { 
      fs.writeFile('text.txt', body); 
      return parseAuths(body); 
     }) 
     .catch(function(err) { 
      // Handle any errors; 
      console.log(err); 
     });   
    } 
-2

您需要從then函數返回promise,並且還應該捕獲請求回調中返回的任何錯誤。試試這個:

}).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' 
     } 
    }; 
    // Need to return a promise 
    return new Promise(function(resolve,reject) { 
     request(options, function(err,httpResponse,body){ 
      if (err) { return reject(err); } 
      return resolve('go again'); 
     }); 
    }); 
+1

這是一個承諾反模式 – Derek

+1

@IceMan只是想說[看藍鳥文檔](http://bluebirdjs.com/docs/anti-patterns.html),Downvoted。 – peteb

+0

如果request()返回一個promise,那麼你可以直接從它返回。如果您的承諾庫支持,您可以使用promisify。否則,這是繼續承諾鏈的方式。 – Shaun