Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - 在以下位置出现未处理的拒绝:TypeError:无法读取未定义的属性“ execute”(Unhandled Rejection at: TypeError: Cannot read property 'execute' of undefined)

The console says the error appears in index.js .

(控制台说错误出现在index.js 。)

I'm trying to export the currency commands code from another file and import it here, just so it looks a bit nicer.

(我正在尝试从另一个文件中导出货币命令代码,并将其导入此处,只是看起来更好一点。)

Error: Unhandled Rejection at: TypeError: Cannot read property 'execute' of undefined

(错误: Unhandled Rejection at: TypeError: Cannot read property 'execute' of undefined)

//index.js
if (command === 'coinflip') {
    client.commands.get('coinflip').execute(message, args);
    }
//coins.js
module.exports = [
{
    name: "coinflip",
    async execute(message, args) {
    //coinflip code here
}}]

If any of you can see the issue (sorry if it's obvious!) and help me out, that would be greatly appreciated!

(如果任何人都可以看到问题(很抱歉,如果很明显!)并帮助我,将不胜感激!)

Thank you.

(谢谢。)

  ask by Miki translate from so

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

1 Answer

0 votes
by (71.8m points)

For the code to work, client.commands must be an object that contains the imported array created in coin.js .

(为了使代码正常工作, client.commands必须是一个包含在coin.js创建的导入数组的对象。)

.get must be a function on the client.commands object that returns the desired object within the coin.js array.

(.get必须是client.commands对象上的一个函数,该函数返回coin.js数组内的所需对象。)

To await the output of execute , it must executed be within an asynchronous function (unless you're using bleeding-edge JS).

(为了等待execute的输出,它必须在异步函数中执行(除非您使用的是最新的JS)。)

You should also execute it in a try/catch block, which will eliminate the 'Unhandled Rejection' error.

(您还应该在try/catch块中执行它,这将消除“未处理的拒绝”错误。)

// index.js

const coins = require('./coin.js')

const client = {
  commands: {
    cmds: [...coins],

    get(cmd) { 
      return commands.cmds.filter(item => item.name === cmd)
    }
  }
}

const doThing = async () => {
  try {
    switch (cmd) {
      case 'coinflip':
        await client.commands.get('coinflip').execute(msg, args)
        break
    }
  } catch (err) {
    console.error(err)
  }
}

;(async () => {
  const cmd = 'coinflip'
  await doThing(cmd)
})()

// coins.js

module.exports = [
  {
    name: "coinflip",
    async execute(message, args) {
      //coinflip code here
    }
  }
]

This seems like overkill though and could be simplified as:

(不过,这似乎有些过分,可以简化为:)

// index.js

const coinflip = require('./coin.js')

const client = {
  commands: {
    coinflip
  }
}

const doThing = async cmd => {
  try {
    await client.commands[cmd].execute(msg, args)
  } catch (err) {
    console.error(err)
  }
}

;(async () => {
  const cmd = 'coinflip'
  await doThing(cmd)
})()

// coins.js

module.exports = {
  async execute(message, args) {
    //coinflip code here
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...