【问题标题】:How do I fix undefined and the arg not reading the correct line in the split?如何修复未定义且 arg 未读取拆分中的正确行?
【发布时间】:2021-05-21 13:55:20
【问题描述】:

所以,我的命令如下所示: +coords SpawnObject( "Truck_01_Covered_Blue", "6642.018555 461.256714 15280.805664", "-163.117828 -4.038700 1.684388" );我的代码是这样的:

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return; 
    
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'ping'){
        message.channel.send('wing!');
    } else if (command === 'coords') {
        var [SpawnObject, BuildingName, pos, yaw] = args; {
            var cords = pos.split(" ")
            var a = yaw.split(" ")
            var x = cords[0]
            var y = cords[1]
            var z = cords[2]
            var a = a[0]
            var b = (a+90) - 360 * Math.floor((a+90)/360)

           console.log(`${`<pos x=${x}" y="${y}" z="${z}" a="${b}" />`}`);
         }
    } 
});

然后我得到这样的结果:

pos x="6642.018555" y="undefined" z="undefined" a="101.25671490000002" /

它会读取第一个数字并放置它,然后它不会读取其他数字,我不知道它从哪里得到 a="101.25671490000002"。

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    不要使用双引号混淆自己,+
    Template literals 可以嵌套。

    你应该改变的行

    message.channel.send(`Co-ords: ${`<pos x="${x}",y="${y}", z="${z}", a="${b}" />`}`);
    

    const SpawnObject = ( pos, coords, yaw) => {
       if (!pos || !coords || !yaw) throw new Error('Invalid Parameter')
       var cords = pos.split(" ")
        var   a = yaw.split(" ")
        var  x = cords[0] || ''
        var  y = cords[1] || ''
        var  z = cords[2] || ''
        var   a = a[0] || ''
        var  b = (a+90) - 360 * Math.floor((a+90)/360) || null
        console.log('Your codes -', `Co-ords: ${("<pos x=" + '"' + x + '"', "y=" + '"' + y + '"', "z=" + '"' + z + '"', "a=" + '"' + b + '"' + " />")}`)
        console.log('Updated codes -', `Co-ords: ${`<pos x="${x}",y="${y}", z="${z}", a="${b}" />`}`);
        
    }
        
        
    SpawnObject("Truck_01_Covered_Blue","8055.317871 208.090668 11192.843750", "-0.037681 -0.921074 6.630672" )

    【讨论】:

    • 我仍然遇到同样的问题。
    • 当你点击 Run code sn-p 时它会工作吗?它是否显示了您的愿望?如果是这样,那么这行代码没有任何问题。该错误可能来自您可能未向我们展示的代码的其他部分。
    • 它不起作用。您没有为其提供从名称中提取并计算每个数字的值并将它们放入响应中的功能。我希望函数从命令中获取这行值(永远不会相同): SpawnObject( "Truck_01_Covered_Blue", "8055.317871 208.090668 11192.843750", "-0.037681 -0.921074 6.630672" ); ,使用该函数获取所有与数学有关的内容并将其拆分,然后将其输出到 message.channel.send(Co-ords: ${}); .
    • 如果您查看发布的问题,您没有给我们任何示例值,我们应该猜测吗?答案纠正了您在message.channel.send 代码中遇到的错误。其他一切都取决于您自己的定制。
    • 我说了我的确切问题,但它仍在发生。即使使用您的“更新代码”,它也会给出与未定义 x 相同的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 2015-07-04
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多