UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性'map'

问题描述 投票:0回答:1

留在家中并保持安全。

我在这里使用node.js请求和request-promise做一个刮板程序,\

我的代码:

const request = require("request");
const cheerio = require("cheerio");
const rp = require("request-promise");

const url = "https://singapore.craigslist.org/d/automotive-services/search/aos"

const scrapeResults = [];

async function scrapeJobHeader() {

    try {

        const htmResult = await rp.get(url);
        const $ = await cheerio.load(htmResult);
        $(".result-info").each((index, element) => {

        const resultTitle = $(element).children(".result-title");
        title = resultTitle.text();
        link = resultTitle.attr("href");
        const datePosted = $(element).children("time").attr("datetime");


        const scrapResult = {title, link, datePosted};
        scrapeResults.push(scrapResult);
        return scrapeResults;

        });

    } catch (err) {

        console.error(err);
    }
}

async function scrapeDescription(jobWithHeaders) {

    return await Promise.all(
        jobWithHeaders.map(async job => {

        const htmResult = await rp.get(job.url);
        const $ = await cheerio.load(htmResult);
        $(".print-qrcode-container").remove();
        job.description = $("#postingbody").text();

    })
    );

}

async function scrapeCraigslist() {

    const jobWithHeaders = await scrapeJobHeader();
    const jobsFullData = await scrapeDescription();
    console.log(jobFullData);
}
scrapeCraigslist();

当我运行代码时,出现类似错误的错误:


C:\Users\Ahmed-PC\craigslist>node index.js
(node:19808) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'map' of undefined
    at scrapeDescription (C:\Users\Ahmed-PC\craigslist\index.js:42:24) 
    at scrapeCraigslist (C:\Users\Ahmed-PC\craigslist\index.js:62:32)  

我如何解决此错误以及我在做什么错?

我是node.js的新手,所以请给我关于错误的简短描述。

谢谢

node.js promise cheerio
1个回答
0
投票

您正在执行此await scrapeDescription();,但是如果不将其传递给数组,则无法调用该函数。

[这样做时,参数jobWithheadersundefined,然后尝试执行undefined.map(),这会给您看到的错误。

似乎您只需要更改此内容:

async function scrapeCraigslist() {

    const jobWithHeaders = await scrapeJobHeader();
    const jobsFullData = await scrapeDescription();
    console.log(jobFullData);
}

至此:

async function scrapeCraigslist() {

    const jobWithHeaders = await scrapeJobHeader();
    const jobsFullData = await scrapeDescription(jobWithHeaders);   // <===
    console.log(jobFullData);
}
© www.soinside.com 2019 - 2024. All rights reserved.