代码之家  ›  专栏  ›  技术社区  ›  Skillzore

如何在助手函数中以干净的方式访问cheerio的`$`?

  •  0
  • Skillzore  · 技术社区  · 7 年前

    我对JavaScript相当陌生,我正在尝试重构它

    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
    });
    

    变成这样:

    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
    });
    

    但这样做会让我犯错误 $ is not defined . 经过 $ 和…在一起 args

    { 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)
    ...
    

    它打印出一个大的物体,上面有几个这样的错误。 $

    1 回复  |  直到 7 年前
        1
  •  1
  •   hbentlov    7 年前

    下一页 未定义传递到getPage函数的变量。它只存在于process1函数的范围内。

    深入研究 Promises . 有了它,您可以链接将在彼此之后运行的方法。在success回调中返回一个新的promise,链中的下一个方法将停止,直到当前promise被解析。

    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
      });