Javascript 此.title返回为未定义

Javascript 此.title返回为未定义,javascript,discord.js,Javascript,Discord.js,我正试图让我的机器人“动态”获取命令名。 标题在模块中定义。导出作为将显示在嵌入中的命令的名称;然而,在一个特定的命令中,我从API中得到了一个笑话,this.title返回为未定义,而不是命令名 它仅在此命令中发生。 我试过只使用title,但它只返回“node”。 我做错了什么? 代码: const Discord=require(“Discord.js”); const{title}=require(“过程”); const config=require(“../data.json”);

我正试图让我的机器人“动态”获取命令名。
标题
模块中定义。导出
作为将显示在嵌入中的命令的名称;然而,在一个特定的命令中,我从API中得到了一个笑话,
this.title
返回为未定义,而不是命令名

它仅在此命令中发生。
我试过只使用
title
,但它只返回“node”。
我做错了什么?
代码:

const Discord=require(“Discord.js”);
const{title}=require(“过程”);
const config=require(“../data.json”);
module.exports={
名字:“爸爸笑话”,
标题:“爸爸的笑话”,
描述:“一个(并非如此)非常有趣的笑话。”,
执行(客户端、消息、参数){
常量https=require('https');
const url=“icanhazdadjoke.com”;
变量选项={
主持人:url,,
港口:443,
路径:“/”,
标题:{“接受”:“应用程序/json”,“用户代理”:“crazyBot”(https://github.com/acrazytown/crazybot)"}
}
函数getData(选项){
https.get(选项,(resp)=>{
让数据=“”;
在('数据',(块)=>{
数据+=块;
});
分别在('结束',()=>{
const embed=new Discord.MessageEmbed()
.setColor(config.franchybot.settings.accent\u color)
.setAuthor(“crazyBot”,config.frazyBot.settings.icon\uURL)
.setTitle(此.title)
.setDescription(JSON.parse(data.joke)
.setFooter(`Powered by${url}`);
message.channel.send(嵌入);
});
}).on(“错误”,(错误)=>{
日志(“错误:+err.message”);
});
}
获取数据(选项)
}
}  

由于函数
getData()
没有对象引用,因此函数中的
this
的值未定义,而
execute()
中的
this
的值定义为要导出的对象。一个简单的解决方案是使用
Function.bind()
getData()
this
关键字设置为对象引用

这是您的
execute()
函数的外观:

execute(client, message, args) {
    const https = require('https');
    const url = "icanhazdadjoke.com";
    var options = {
        host: url,
        port: 443,
        path: "/",
        headers: {"Accept":"application/json", "User-Agent":"crazyBot (https://github.com/acrazytown/crazybot)"}
    }

    var getData = function(options) {
        https.get(options, (resp) => {
        let data = '';

        resp.on('data', (chunk) => {
            data += chunk;
        });

        resp.on('end', () => {
            const embed = new Discord.MessageEmbed()
                .setColor(config.crazyBot.settings.accent_color)
                .setAuthor("crazyBot", config.crazyBot.settings.icon_url)
                .setTitle(this.title)
                .setDescription(JSON.parse(data).joke)
                .setFooter(`Powered by ${url}`);
            
            message.channel.send(embed);
        });

        }).on("error", (err) => {
        console.log("Error: " + err.message);
        });
    }.bind(this);

    getData(options)
}
相关资源:

由于函数
getData()
没有对象引用,因此函数中的
this
的值未定义,而
execute()
中的
this
的值定义为要导出的对象。一个简单的解决方案是使用
Function.bind()
getData()
this
关键字设置为对象引用

这是您的
execute()
函数的外观:

execute(client, message, args) {
    const https = require('https');
    const url = "icanhazdadjoke.com";
    var options = {
        host: url,
        port: 443,
        path: "/",
        headers: {"Accept":"application/json", "User-Agent":"crazyBot (https://github.com/acrazytown/crazybot)"}
    }

    var getData = function(options) {
        https.get(options, (resp) => {
        let data = '';

        resp.on('data', (chunk) => {
            data += chunk;
        });

        resp.on('end', () => {
            const embed = new Discord.MessageEmbed()
                .setColor(config.crazyBot.settings.accent_color)
                .setAuthor("crazyBot", config.crazyBot.settings.icon_url)
                .setTitle(this.title)
                .setDescription(JSON.parse(data).joke)
                .setFooter(`Powered by ${url}`);
            
            message.channel.send(embed);
        });

        }).on("error", (err) => {
        console.log("Error: " + err.message);
        });
    }.bind(this);

    getData(options)
}
相关资源:

好吧,通过
JSON.parse()
传递累积的字符串
数据
,得到实际的笑话;也许那也是标题所在的地方。不清楚为什么您希望“title”是导出的主对象的属性。我刚刚检查过,
数据中没有标题,只有笑话、笑话id和状态。我希望
this.title
能够工作,因为它在其他所有命令中都能工作,而在这个命令中却不能。您在没有任何对象引用的情况下调用
getData()
,因此
this
本身将是
未定义的
。那么我如何才能获得标题呢?抱歉,如果这是一个非常愚蠢的问题,我仍在学习JSWell如果“title”应该是导出对象中“title”属性的值(“爸爸的笑话”),那么您可以向
getData()
添加一个参数,并将
this.title
传递到函数中,或者在
execute()中使用一个
title
变量
获取对象的“title”属性的副本(例如,
让title=this.title;
)。您可以通过
JSON.parse()传递累积的字符串
数据
,以获得实际的笑话;也许那也是标题所在的地方。不清楚为什么您希望“title”是导出的主对象的属性。我刚刚检查过,
数据中没有标题,只有笑话、笑话id和状态。我希望
this.title
能够工作,因为它在其他所有命令中都能工作,而在这个命令中却不能。您在没有任何对象引用的情况下调用
getData()
,因此
this
本身将是
未定义的
。那么我如何才能获得标题呢?抱歉,如果这是一个非常愚蠢的问题,我仍在学习JSWell如果“title”应该是导出对象中“title”属性的值(“爸爸的笑话”),那么您可以向
getData()
添加一个参数,并将
this.title
传递到函数中,或者在
execute()中使用一个
title
变量
获取对象的“title”属性的副本(例如,
让title=this.title;
)。