如何让 cheerio 的 `$` 以一种干净的方式在辅助函数中访问?
How to make cheerio's `$` accessible in helper functions in a clean way?
我是 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
一起传递给我一个来自 cheerio 的错误(或者至少我认为它来自 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)
...
它打印出一个大对象,其中有几个类似的错误。
那么,我做错了什么?有没有比传递 $
更简洁的方法?
报错是因为传入getPage函数的nextPage变量没有定义。它只存在于 process1 函数的范围内。
深入了解 Promises。有了它,您可以将 运行 的方法串联起来。 Return 成功回调中的新承诺,链中的下一个方法将停止,直到当前承诺得到解决。
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
});
我是 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
一起传递给我一个来自 cheerio 的错误(或者至少我认为它来自 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)
...
它打印出一个大对象,其中有几个类似的错误。
那么,我做错了什么?有没有比传递 $
更简洁的方法?
报错是因为传入getPage函数的nextPage变量没有定义。它只存在于 process1 函数的范围内。
深入了解 Promises。有了它,您可以将 运行 的方法串联起来。 Return 成功回调中的新承诺,链中的下一个方法将停止,直到当前承诺得到解决。
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
});