/    Sign up×
Community /Pin to ProfileBookmark

TypeError: Cannot read property ‘replace’ of undefined

So I tried to learn how to do this on my own by taking two video courses about node and cheerio on udemy.com but I haven’t gotten any better at it. The code produces an array of image URLs when I do this:

“`
const request = require(“request-promise”);
const cheerio = require(“cheerio”);

const url = “https://www.example.com”;

const scrapeResults = [];
async function scrapeJobHeader() {
try {
const htmlResult = await request.get(url);
const $ = await cheerio.load(htmlResult);
$(“td.productListing-data > a “).each((index, element) => {
const resultTitle = $(element).children(“img”);

const img_url = resultTitle.attr(“src”);

const scrapeResult = { img_url };

scrapeResults.push(scrapeResult);
});
return scrapeResults;
} catch (err) {
console.error(err);
}
}

async function scrapeWebsite() {
const jobsWithHeaders = await scrapeJobHeader();
console.log(jobsWithHeaders);

}

scrapeWebsite();

“`

but I get the error “TypeError: Cannot read property ‘replace’ of undefined” when I do this:

`const img_url = resultTitle.attr(“src”).replace(“images\/more_color.png”,””);
`

The images that are returned include “images/more_color.png” but I just want to return the actual product images and not the pngs.

The results look like this:

“`
{ img_url: ‘images/more_color.png’ },
{ img_url: undefined },
{ img_url: undefined },
{
img_url: ‘images/20191206/thumb/AK1501-@RH-CRY-LOVE@22X06-825_3L@467400@200@[email protected]
},
{ img_url: ‘images/more_color.png’ },
{ img_url: undefined },
{ img_url: undefined },
{
img_url: ‘images/20191206/thumb/AK1501-@GD-CRY-LOVE@22X06-825_3L@467399@200@[email protected]
},
{ img_url: ‘images/more_color.png’ },
{ img_url: undefined },
{ img_url: undefined },
{
img_url: ‘images/20191206/thumb/AK1500-@GD-CRY-QUEEN@3X06-825_3L@467397@200@[email protected]
},
{ img_url: ‘images/more_color.png’ },
{ img_url: undefined },
{ img_url: undefined },

“`
The next step would be to get rid of the undefined image URLs but I haven’t gotten that far yet.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@makamo66authorApr 01.2020 — I got rid of the undefined ones by using:


``<i>
</i> if (img_url != undefined){
scrapeResults.push(scrapeResult);
}<i>
</i>
``
Copy linkTweet thisAlerts:
@makamo66authorApr 01.2020 — The solution to this problem was published at https://www.freecodecamp.org/forum/t/web-scraper-outputs-just-one-item-instead-of-many/360427/46
×

Success!

Help @makamo66 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...