Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

node.js - nodejs cheerio returns undefined although it found a text

I want to search a Word and the Link. If it found, then I want to return the link. Everything works fine if I output it to console.log. If I make an object, and add the Link to the object and if I return it, then I get undefined. Why? Cheerio found my word successfully (I tested with console.log)

I want to add the found links into an object. But it doesn't work

'use strict';

const superagent = require('superagent');

const cheerio = require('cheerio');

const email = domain => {
    const email_obj = {
        email_exists: false,
        email_link: false
    };

    superagent
    .get(domain)
    .end((err, res) => {
        let $ = cheerio.load(res.text);
        let links = $('a');

        $(links).map((i, link) => {
            const data = $(link).text().toLocaleLowerCase().trim();
            if (data.includes('e-mail') && $(link).attr('href') != '' && $(link).attr('href') != null) {
                email_obj.imprint_exists = true;
                email_obj.imprint_link = $(link).attr('href');
                console.log($(link).attr('href'));
            }
        });
    });
    return email_obj;
}

module.exports = email;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...