I am fairly new to JavaScript and I am trying to refactor this
const rp = require('request-promise');
const cheerio = require('cheerio'); // Basically jQuery for node.js
// shared function
function getPage(url) {
const options = {
uri: url,
transform: function(body) {
return cheerio.load(body);
}
};
return rp(options);
}
getPage('https://friendspage.org').then($ => {
// Processing 1
const nxtPage = $("a[data-url$='nxtPageId']").attr('data');
return getPage(nxtPage).then($ => {
// Processing 2
});
}).catch(err => {
console.log(err);
// error handling here
});
into something like this:
const rp = require('request-promise');
const cheerio = require('cheerio'); // Basically jQuery for node.js
// shared function
function getPage(url) {
const options = {
uri: url,
transform: function(body) {
return cheerio.load(body);
}
};
return rp(options);
}
function process1(args) {
// Processing 1
return $("a[data-url$='nxtPageId']").attr('data');
}
function process2(args) {
// Processing 2
}
getPage('https://friendspage.org').then($ => {
const nxtPage = process1(args);
return getPage(nxtPage).then($ => {
process2(args);
});
}).catch(err => {
console.log(err);
// error handling here
});
but doing this, gets me the error $ is not defined
. Passing $
in with the args
gets me an error from cheerio (or at least I think it is from cheerio):
{ RequestError: Error: options.uri is a required argument
at new RequestError (C:\Users\Skillzore\git\projects\gadl\node_modules\request-promise-core\lib\errors.js:14:15)
at Request.plumbing.callback (C:\Users\Skillzore\git\projects\gadl\node_modules\request-promise-core\lib\plumbing.js:87:29)
at Request.RP$callback [as _callback] (C:\Users\Skillzore\git\projects\gadl\node_modules\request-promise-core\lib\plumbing.js:46:31)
at self.callback (C:\Users\Skillzore\git\projects\gadl\node_modules\request\request.js:185:22)
at Request.emit (events.js:182:13)
...
It prints a big object with several errors like that.
So, what am I doing wrong? And is there a cleaner way to do this than passing $
around?