首页 > 解决方案 > 参数中的节点JS未定义变量

问题描述

let args = message.content.slice(config.prefix.length).split(' ');
let command = args.shift().toLowerCase();
let tellJoke = randomNumber(4);

if (debounce == true){
    return
  }
debounce = true

switch (command) {

  case 'gameid':
    console.log(args.length)
    if(args.length == 1){
      let placeId = args[1]
      console.log(placeId)
      api = await fetch("https://api.roblox.com/universes/get-universe-containing-place?placeid=" + placeId)
      let gameId = await api.json()
      try{
        if(api){
          reply = await message.reply('Game Id: ' + gameId.UniverseId)
          debounce = false
          break;
        }
      }catch(error){
        reply = await message.reply('Something went wrong. ERROR: ' + error);
        debounce = false
        break;
      }

      }
    reply = await message.reply('Something went wrong. Did you write the command correctly?')
    debounce = false
    return

当我清楚地将地点 ID 放在那里时,它说 placeId/arg 1 未定义。有谁知道为什么会这样?我已经打印/记录了它的参数和长度,我可以说它应该可以工作。

标签: node.js

解决方案


数组索引从0. 因此,如果数组的长度为 1,您可以使用 . 访问第一个元素args[0]

let args = message.content.slice(config.prefix.length).split(' ');
let command = args.shift().toLowerCase();
let tellJoke = randomNumber(4);

if (debounce == true){
    return
  }
debounce = true

switch (command) {

  case 'gameid':
    console.log(args.length)
    if(args.length == 1){
      let placeId = args[0]
      console.log(placeId)
      api = await fetch("https://api.roblox.com/universes/get-universe-containing-place?placeid=" + placeId)
      let gameId = await api.json()
      try{
        if(api){
          reply = await message.reply('Game Id: ' + gameId.UniverseId)
          debounce = false
          break;
        }
      }catch(error){
        reply = await message.reply('Something went wrong. ERROR: ' + error);
        debounce = false
        break;
      }

      }
    reply = await message.reply('Something went wrong. Did you right the command correctly?')
    debounce = false
    return

推荐阅读