【问题标题】:TypeError: Cannot read property 'coins' of null Discord.js errorTypeError:无法读取 null Discord.js 错误的属性“硬币”
【发布时间】:2021-07-13 16:43:47
【问题描述】:
const db = require('quick.db');
module.exports = {
    name: 'bal',
    description: "Check your coin balance.",
    execute(message, args) {
        let profile = db.get(`${message.author.id}`)
        if (!profile.coins) return message.lineReply('You currently have 0 coins.')
        message.lineReply(`You currently have ${profile.coins} coins.`);
    }
}

这是我的 discord.js 机器人的 balance 命令的命令文件。 该脚本在控制台中输出:

TypeError: Cannot read property 'coins' of null

这里是关于数据库的更多信息:https://quickdb.js.org/

【问题讨论】:

  • 看起来 db.get() 可能是一个 Promise,可能需要等待或用 .then 链接
  • @Hyetigran - 不,如果是这种情况,profile.coins 将是 undefined,而不是错误。错误说profilenull
  • 错误告诉你profilenull。所以你必须找出为什么db.get(`${message.author_id}`) 会返回null。 (我对此感到惊讶,因为像 Hyetigran 一样,我本来希望它返回一个承诺,但也许它是一个老式的基于回调的 API。)(旁注:db.get(`${message.author_id}`) 是一种复杂的编写方式 @987654333 @. :-) )
  • 请添加有关您使用的数据库的详细信息,只有这样我们才能完全诊断您的问题。
  • 我们能否也看到您的数据库设置代码。没有它,我们就不能再进一步了。

标签: javascript node.js discord.js


【解决方案1】:

TypeError: Cannot read property 'some object' of null

您正在访问一个为空的对象的属性。例如,document.getElementById('stuff') 返回 null。所以添加.examplevalue会报错。

在访问其属性之前测试对象是否有效。 (有问题的对象是profile)。

您的 db.get() 返回 null,因为没有值。确保 A) 您的数据库设置正确,并且 B) 访问所述数据库的语法也是正确的。否则肯定会抛出这个错误。

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 2021-03-02
    • 2021-09-25
    • 2021-02-27
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2020-01-29
    相关资源
    最近更新 更多