function runScript(url) {
nightmare
.goto(url)
.evaluate(function)
.then(function)
.catch(function(err) {
console.error(err) // <<< This gets called.
return nightmare.end() // <<< this does not work.
})
.then()
.catch(function(err){}) // <<< this does not get called.
}
runScript('http://www.faaaaacebook.com') // wrong url
@mingsterism Two things:
catch()
or the second argument to then()
, that resolves things in the promise chain so they are no longer in an error state (if you throw an error or return a promise that rejects from catch()
, though, that will put the promise chain back in to an error state).It might help to think of it like synchronous code. This:
.then(function(result) {
// do something A
})
.catch(function(error) {
// handle error 1
})
.then(function(result) {
// do something B
})
.catch(function(error) {
// handle error 2
})
Is kind of like:try {
try {
// do something A
}
catch(error) {
// handle error 1
}
// do something B
}
catch(error) {
// handle error 2
}
nightmare.end()
call is not working, or was that just confusion from the above issue? Everything you're doing looks correct there.
.then()
function was nested as per your try catch
code.
.then()
means there are 5 nested levels?
then
also
then
OR catch
will run
then
, of course)
.evaluate(function) // << error here
.then(function()) // << does not get called
.catch( return nightmare.end())
.then(return nightmare.end())
end()
more than once
.evaluate(something) // maybe error, maybe not
.then(function(result) {
// do what you need with result, then:
return nightmare.end();
})
.catch(function() {
return nightmare.end()
})
then
:)
ERRRRRRRRRRRRRRR
is called. but nightmare instance does not close.
nightmare
is not a local variable there
nightmare.end()
it is probably throwing an error
self.nightmare.end()
ctrl-c
. does it mean nightmare is still running?
getTitlesAndNextUrl
shouldn't be exposed on an instance
nightmare-swiftly
works, and how it uses .use()
to create reusable action chains
.then(function(v) {
console.log(v.nextUrl, '===========++++++')
Promise.all(v.titles.map(function(title) {
new self.model({job: title}).save((err, result) => {
if (err) console.error(err);
console.log(result);
})
}))
return {
nextUrl: v.nextUrl
}
})
Could just be .then(function(v) {
console.log(v.nextUrl, '===========++++++')
return Promise.all(v.titles.map(function(title) {
new self.model({job: title}).save()
})).then(function() {
return {nextUrl: v.nextUrl}
});
})
return v.nextUrl
instead of an object with only that in it return {nextUrl: v.nextUrl}
Promise.all
there at all)
.then()
inside the outer .then(function(v)
function saveTitles(data) {
return Promise.all(data.titles.map(function(title) {
new self.model({job: title}).save()
}))
}
// later...
.then(saveTitles)
.then(function(data) {
return saveTitles(data).then(Promise.resolve(data.nextUrl));
})
then()
much
require()
calls out of the constructorArray.prototype.slice.call(document.querySelectorAll(...))
can just be Array.from(document.querySelectorAll())
function sum(a, b) {
return [a + b];
}
in the case of web scraping tasks, is reactive useful?
Maybe. I don't think it's utility is particular to scraping
then...catch
problem was fixed
.then()
was of the form .then(resolve, reject)
?
.then().catch()
.then(resolve, reject)
is considered an antipattern by some
var nightmare = Nightmare({
webPreferences: {
preload: custom-script.js
}
})
I tried it that way, and even after adding
window.__nightmare = {};
__nightmare.ipc = require('ipc');
at the beginning of the file, my nightmare is stuck
'ipc'
module locally as well? (Isn't the preload script something evaluated in the browser?)
is custom-script.js the path to a file?
yes.
do i need to install the
'ipc'
module locally as well?
no.
(Isn't the preload script something evaluated in the browser?)
yes, but it runs prior to almost the entirety of the render cycle.
preload
scripts
preload
hooks up the error forwarding and some other sugar for modal dialogs that you'd have to set up yourself if you're using them
require('electron').ipcRenderer
require('ipc')
will work
nightmare.on("console", ...)
don't seem to forward console.log
in the preload script (maybe that's waht you meant earlier)
src
to ''
after DOM is loaded) to gain time, but it seems that only something like editing the html received before it's processed would help
webPreferences
... facepalm