简体   繁体   English

“ typeError:无法读取未定义的属性“用户名”。”

[英]“typeError: Cannot read property 'username' of undefined.”

Here is my code for the >top10 command of my discord bot: 这是我的discord机器人的>top10命令的代码:

const Discord = require("discord.js");

module.exports.run = async(bot, message, args, con) => {

  const top10query = `SELECT user, points, lstmsg FROM scores WHERE guild = '${message.guild.id}' ORDER BY cast(points as SIGNED) ASC LIMIT 10`
  //const top10 = con.query(top10query)

  const query = querytxt => {
    return new Promise((resolve, reject) => {
      con.query(querytxt, (err, results, fields) => {
        if (err) reject(err);
        resolve([results, fields]);
      });
    });
  };
  const [results, fields] = await query(top10query);

  const map1 = results.map(results => `User: ${(bot.fetchUser(results.user)).username} Messages: ${results.points} Last message: ${results.lstmsg}`);
  message.channel.send(map1)
}
module.exports.help = {
  name: "top10",
  usage: "``prefix`` top10",
  description: "top 10 points",
}

The data for "user" is stored as the user ID. “用户”的数据存储为用户ID。 I get 'undefined' when I use the >top10 command. 当我使用> top10命令时,我得到“未定义”。

未定义的用户

Any ideas? 有任何想法吗?


EDIT: 编辑:
I've tried replacing 我尝试更换

const map1 = results.map(results => `User: ${(bot.fetchUser(results.user)).username} Messages: ${results.points} Last message: ${results.lstmsg}`);
      message.channel.send(map1)

With

const map1 = results.map(results => `User: ${(bot.users.get(results.user.id)).username} Messages: ${results.points} Last message: ${results.lstmsg}`);

But I'm getting this error: 但我收到此错误:

(node:21128) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'username' of undefined

Replace 更换

 User: ${(bot.fetchUser(results.user)).username 

with: 有:

 User: ${bot.users.get(results.user).username 

You want to do 你想做

const map1 = await results.map(async result => {
        const user = await client.fetchUser(result);
        return `User: ${user.username} Messages: ${result.points} Last message: ${result.lstmsg}`; 
});

暂无
暂无

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

相关问题 TypeError:无法读取未定义的属性“ get”。 是npm的问题吗? - TypeError: Cannot read property 'get' of undefined. Is npm the issue? TypeError:无法读取未定义的属性“ 0”。 React Redux应用 - TypeError: Cannot read property '0' of undefined. React Redux App 未处理的拒绝(TypeError):无法读取未定义的属性“扩展”。 (GraphQL) - Unhandled Rejection (TypeError): Cannot read property 'extensions' of undefined. (GraphQL) × TypeError:无法读取未定义的属性“地图”。 发现错误 - × TypeError: Cannot read property 'map' of undefined. found error TypeError:无法读取未定义的属性“地图”。 反应 - TypeError: Cannot read property 'map' of undefined. React 错误类型错误:无法读取未定义的属性“toLowerCase”。 离子 3 - ERROR TypeError: Cannot read property 'toLowerCase' of undefined. Ionic 3 TypeError:无法读取未定义的属性“数量”。 反应原生 Redux - TypeError: Cannot read property 'qty' of undefined. React Native Redux TypeError:无法读取未定义的属性'addTopic'。 我是盲人吗? - TypeError: Cannot read property 'addTopic' of undefined. Am I blind? 无法读取未定义的属性“toFixed”。 未捕获的类型错误 - Cannot read property 'toFixed' of undefined. Uncaught TypeError Node / AngularJS-TypeError:无法读取未定义的属性“ _id”。 - Node/AngularJS - TypeError: Cannot read property '_id' of undefined.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM