我当前在async / await函数中使用replace
方法,并且在控制台中收到错误日志。我在YellowPages上进行了一些抓取,到目前为止,我已经有了所需的内容,但是当尝试通过删除'mailto:[email protected]'
来清理像这样的字符串"mailto:"
来清除电子邮件数据时,出现以下错误:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'replace' of undefined
at Node.<anonymous> (/Users/aeum3893/Documents/test_scrapping/yellowpages_scraping/index.js:20:40)
(node:25401) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 8)
这是代码段
const getCompanies = async () => {
try {
const html = await rp(baseURL + searchURL);
const businessMap = cheerio('a.business-name', html).map(async (i, e) => {
const link = baseURL + e.attribs.href;
const innerHtml = await rp(link);
const emailAddress = cheerio('a.email-business', innerHtml).prop('href')
const email = emailAddress.replace(/mailto:/, '');
const name = e.children[0].data || cheerio('h1', innerHtml).text();
const phone = cheerio('p.phone', innerHtml).text();
const metadata = {
emailAddress,
link,
name,
phone,
}
parsedResults.push(metadata)
})
删除删除方法后,一切正常。
感谢您的帮助,谢谢!