繁体   English   中英

简单的 function 从 html 返回未定义的 href 链接,但在被要求时打印链接

[英]simple function that gets href link from html returning undefined, but prints link when asked to

所以我正在尝试制作一个发送链接的 discord 机器人。 我有这段代码可以抓取网站并将 href 链接发送到 discord。

function scrapeForLink(recievedMessage){
    let scienceDailyURL = "https://www.sciencedaily.com";
    let link;

     request("https://www.sciencedaily.com/news/computers_math/artificial_intelligence/",
        (error, response, html) => {
            let success = !error && response.statusCode === 200;

            if (success){
                const $ = cheerio.load(html);

                link = $('#featured_tab_1').find('.latest-head').find('a').attr('href');
        }
        recievedMessage.channel.send(scienceDailyURL + link);
        console.log(scienceDailyURL + link)
}

当这个 function 执行时,它会工作,它会发送链接并将其打印到控制台,但是当我尝试返回它(我需要在机器人中稍后取得进展)时,它返回未定义:

function scrapeForLink(recievedMessage){
    let link;
    let scienceDailyURL = "https://www.sciencedaily.com";

    request("https://www.sciencedaily.com/news/computers_math  /artificial_intelligence/",
    (error, response, html) => {
        let success = !error && response.statusCode === 200;

        if (success){
            const $ = cheerio.load(html);

            link = $('#featured_tab_1').find('.latest-    head').find('a').attr('href');
    }
    recievedMessage.channel.send(scienceDailyURL + link);
    return(scienceDailyURL + link);
}

执行此操作时,它仍然发送链接,但是当我将 function 打印到控制台时,它返回 undefined

这是执行时的 function:

function processCommand(recievedMessage) {
    let fullCommand = recievedMessage.content.substr(1);
    let splitCommand = fullCommand.split(" ");
    let primaryCommand = splitCommand[0];
    let args = splitCommand.slice(1);

    if (primaryCommand === "multiply") {
        multiplyCommand(args, recievedMessage)
    }
    else if (primaryCommand === "github"){
        recievedMessage.channel.send("https://github.com/Pacutacatete100/AINewsBot/blob/master/MediaBot.js")
    }
    else if (primaryCommand === "add") {
        addCommand(args, recievedMessage)
    }
    else if(primaryCommand === "factorial"){
        factorial(args, recievedMessage)
    }
    else  if (primaryCommand === "news"){
        scrapeForLink(recievedMessage)//this is where i call the function, 
//if i am trying to print the value of the function would do console.log(scrapeForLink(recievedMessage))
}

}

您正在从回调 function 返回值,但该值并没有真正“返回”到任何东西。 请求完成时会调用回调 function ( request的第二个参数),您将获得链接。 如果你想使用它,你应该在回调 function 中这样做。

请记住,您的回调 function 在具有最终数据时由request function 调用,但无论您的回调 function 返回什么,它都不会做任何事情。

您可以使用link变量,甚至可以将另一个变量设置为该值,但请记住,由于它是一个回调 function,它可能会在调用request的 function 完成后执行。

let link;
let scienceDailyURL = "https://www.sciencedaily.com";

let x = request("https://www.sciencedaily.com/news/computers_math/artificial_intelligence/", 
 (error, response, html) => {
  let success = !error && response.statusCode === 200;
  if (success){
    const $ = cheerio.load(html);
    link = $('#featured_tab_1').find('.latest-head').find('a').attr('href');
  }
  recievedMessage.channel.send(scienceDailyURL + link);
  // This is where you can go ahead and do something meaningful with 'link'
  someFunctionThatUsesLink(scienceDailyURL + link);
  // The return statement doesn't do anything since the caller is the 'request' function.
  return(scienceDailyURL + link);
});

// x will be undefined

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM