petecoop / phantasma

A high level promise based wrapper for phantomjs
ISC License
33 stars 8 forks source link

trouble with each #34

Open skaplun opened 8 years ago

skaplun commented 8 years ago

Hey, sorry I'm bombing you :) thanks for the help so far :) i ran into another issue,essentially everything works but i get an error on each test at some random point (seems different in each trial):

node : events.js:72
At line:1 char:1
+ node hello3.js
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (events.js:72:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

      listeners[i].call(self);
                  ^
TypeError: Cannot read property 'call' of undefined
    at emitNone (events.js:72:19)
    at SockJSConnection.emit (events.js:166:7)
    at cleanup (C:\Users\shai\node_modules\shoe\index.js:25:20)
    at Session.stream._session.didTimeout (C:\Users\shai\node_modules\shoe\index.js:14:13)
    at Session.timeout_cb [as _onTimeout] (C:\Users\shai\node_modules\sockjs\lib\transport.js:101:22)
    at Timer.listOnTimeout (timers.js:92:15)

My Code:

ph.viewport(1360, 960)
    .open(domain)
    .screenshot(dir + '\\home.png')
    .evaluate(function () {

    return [].filter.call(document.querySelectorAll('a'), function (link) {
       if(link.hostname === document.location.hostname) {
           return link
        }

     }).map(function(link){
       return [link.hostname, link.pathname];
    })

})
.catch(function (e) {
    console.log('error', e);
})
.finally(function (results) {
    console.log('finally!');
    ph.exit();
    return results;

}).each(function(link){
    return scrapUrl(link, dir)
})
petecoop commented 8 years ago

Not too sure, could be how you are doing catch/finally before the each, probably best to wrap that e.g.

function doThing(domain) {
  return ph.open(domain)
    .evaluate() //etc etc
    .catch()
    .finally()
}

doThing(domain)
  .each(function (results) {
    //do something with results
  });

Btw you can rewrite to:

return [].filter.call(document.querySelectorAll('a'), function (link) {
  return link.hostname === document.location.hostname;
})

As filter just takes true or false

skaplun commented 8 years ago

I get the same error when i try:

  1. doThing(domain).each(..)
  2. doThing(domain).then(function(results){ Promise.each(results, ...) })
  3. doThing(domain).then(function(results){ Promise.mapSeries(results, ...) })

where do you think the problem is likely? :)

petecoop commented 8 years ago

Can you try changing the code in the evaluate method, could be something wrong with that

skaplun commented 8 years ago

I'm not following? the filter in evaluate produces a correct list of urls, its constructed this way to convert node list to array.. unless I'm missing something?

petecoop commented 8 years ago

what I'm trying to establish is whether the issue is within that or somewhere else, so if you have it just return a mock of the values you want instead we can see

skaplun commented 8 years ago

tried to run test with a list i scraped from google with 17 links and ran into same error after 12 links