我在请求应用程序上进行了简单的 Cheerio 解析。不知道为什么在尝试设置数组时会出现这个未定义的错误,但我猜该值不适合设置。
var $ = cheerio.load(body);
var json = [
{ "range": "", "address": "", "state": "", "zip": "", "info": "" }
];
$('.findCourse').each(function (i, elem) {
// Range Name
console.log("iteration - ", i);
console.log("name - ", $(this).text().trim());
json[i].range = $(this).text().trim();
});
这是我的控制台响应,它读取并设置在抓取的 html 中找到的前两项。
iteration - 0
name - Pollock's Ferry Hunting Club Inc.
iteration - 1
name - Eagle 1
TypeError:无法设置未定义的属性“范围”
at Object.<anonymous> (/usr/local/node_app/server.js:30:31) at exports.each (/usr/local/node_app/node_modules/cheerio/lib/api/traversing.js:267:24) at Request.request.post.form.__EVENTTARGET [as _callback] (/usr/local/node_app/server.js:26:30) at Request.self.callback (/usr/local/node_app/node_modules/request/request.js:121:22) at Request.EventEmitter.emit (events.js:98:17) at Request.<anonymous> (/usr/local/node_app/node_modules/request/request.js:978:14) at Request.EventEmitter.emit (events.js:117:20) at IncomingMessage.<anonymous> (/usr/local/node_app/node_modules/request/request.js:929:12) at IncomingMessage.EventEmitter.emit (events.js:117:20) at _stream_readable.js:920:16 1 May 23:02:57 - [nodemon] app crashed - waiting for file changes before starting...