2016-11-23 28 views
2

我使用Node和npm phantom來製作很多網頁的屏幕截圖。 爲了節省CPU,我爲每個進程使用一個Phantom實例。在這種情況下,我打開一個頁面,渲染它,關閉它並繼續。Node + Promise + Phantom.JS - 奇怪的錯誤

除非我開始另一個過程,否則這種方法非常好。在這種情況下,我得到這個錯誤,當一個過程完成時:

UnhandledPromiseRejectionWarning:未處理的承諾拒絕(拒絕ID:2):錯誤:未定義不是一個對象(評估'目標[command.params [0]]' )

幻影例如:

function initPhantom(todos) { 
    phantom.create(['--ignore-ssl-errors=no'], {logLevel: 'error'}) 
     .then(function (instance) { 
      var processId = instance.process.pid; 
      console.log("===================> instance: ", processId); 
      phInstance = instance; 
      webshot(0, todos); 
     }); 
} 

遍歷網址:

function webshot(id, shots) { 
     phInstance.createPage().then(function (_page) { 
       sitepage = _page; 
       sitepage.property('viewportSize', {width: 1024, height: 768}); 
       WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36'); 
       sitepage.setting("resourceTimeout", 4000); 
       return sitepage.open(shots[id].url); 
      }) 
      .then(function (status) { 
       console.log("render %s/%s", id + 1, shots.length); 
       setTimeout(function() { 
        var image = 'temp_img/' + shots[id]._id + '.png'; 
        sitepage.render(image, {format: 'png', quality: '30'}).then(function (finished) { 
         sitepage.close(); 
         chekcImageinDb(shots[id], image); 
         if (id < shots.length - 1) { 
          id += 1; 
          webshot(id, shots); 
         } else { 
          console.log("===================> all done: %s files has been written", shots.length); 
          phInstances.shift(); 
          if(phInstances.length < 1){ 
           console.log("kill phantom ", phInstances); 
           //phInstance.exit(); 
          } 
         } 
        }); 
       }, 2000); 
      }) 
    } 

任何想法

--------編輯-----------

好的我試過了你的建議,同樣的錯誤。正如你在日誌中看到的,它只發生在一個過程完成後。

function webshot(id, shots) { 
     phInstance.createPage().then(function (_page, error) { 
      if(error){ 
       console.log("first", error); 
      } 
       sitepage = _page; 
       sitepage.property('viewportSize', {width: 1024, height: 768}); 
       WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36'); 
       sitepage.setting("resourceTimeout", 4000); 
       return sitepage.open(shots[id].url); 
      }).catch(function(e) { 
       console.log(e); 
      }).then(function (status, error) { 
       if(error){ 
        console.log(error) 
       } 
       console.log("render %s/%s", id + 1, shots.length); 
       setTimeout(function() { 
        var image = 'temp_img/' + shots[id]._id + '.png'; 
        sitepage.render(image, {format: 'png', quality: '30'}) 
         .then(function (finished, error) { 
         if(error){ 
          console.log(error) 
         } 
         sitepage.close(); 
         chekcImageinDb(shots[id], image); 
         if (id < shots.length - 1) { 
          id += 1; 
          webshot(id, shots); 
         } else { 
          console.log("===================> all done: %s files has been written", shots.length); 
          phInstances.shift(); 
          if(phInstances.length < 1){ 
           console.log("kill phantom ", phInstances); 
           //phInstance.exit(); 
          } 
         } 
        }).catch(function(e) { 
         console.log(e); 
        }); 
       }, 2000); 
      }).catch(function(e) { 
      console.log(e); 
     }) 
    } 

以下是幾乎同時啓動的兩個實例的日誌文件。就像我說的,如果只有一個進程正在運行,那麼一切都很好。

enter image description here

回答

1

我認爲你需要一個第二個回調函數傳遞給失敗處理您的then函數調用。取決於接口,您可能需要在then()呼叫後鏈接.catch(function(){})

+0

感謝您的快速回答。我已經嘗試用戶.catch(),但我會嘗試它。 –

+0

請使用'.catch()'更新您的代碼並進行錯誤處理。 –

+0

現在好了,它正在捕獲第二個ctach上的錯誤。任何想法如何處理這個? –