How to make cheerio's `$` accessible in helper

2019-08-26 15:08发布

问题:

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?

回答1:

The error is displayed because the nextPage variable passed into the getPage function is not defined. It only lives in the scope of the process1 function.

Take a deeper look at Promises. With it, you can chain methods that will be run after each other. Return a new promise in the success callback, and the next method in the chain will halt until the current promise is resolved.

function process1($) {
  // Process stuff
  // What you return here will be passed to the next function in the promise chain below (in this case a string)
  return $("a[data-url$='nxtPageId']").attr('data');
}

function process2(nextPage) {
  // More processing
  // getPage will return a promise which eventually gets resolved with the cheerio object
  return getPage(nextPage);
}

function process3($) {
  // More processing?
}

getPage('https://friendspage.org')
  .then(process1)
  .then(process2)
  .then(process3)
  .catch(err => {
      console.log(err);
      // error handling here
  });