TypeError: Cannot read properties of undefined (reading ‘NAME‘)报错解决
Posted 胡歌1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError: Cannot read properties of undefined (reading ‘NAME‘)报错解决相关的知识,希望对你有一定的参考价值。
一、错误查找
问题描述:前端一个el-table表格,一个医院查询到的科室从后端返回时总是显示不出来,response里面是有数据的,这个表格别的医院都能显示出科室,就那个医院显示不出。报错:TypeError: Cannot read properties of undefined (reading 'NAME')
查找问题所在,发现el-table里面有一个:formatter="formatter_DepType",具体方法如下:
const formatter_DepType = function (row, column)
if (_this.Type_Ary && row.DEP_TYPE)
return _this.Type_Ary.filter((data) => data.CODE == row.DEP_TYPE)[0].NAME;
return "";
其中Type_Ary里面的内容是这样的:
Type_Ary: [ CODE: '1', NAME: '门诊科室' , CODE: '2', NAME: '住院科室' , CODE: '3', NAME: '其他' ],
它定义子在data里面。
这样就发现错误了,科室的DEP_TYPE查询出来之后如果有和Type_Ary里面的数据不一致的就会直接报错。
二、问题解决
只需要在方法上加个判断就行了:
const formatter_DepType = function (row, column)
if (_this.Type_Ary && row.DEP_TYPE)
if(typeof(_this.Type_Ary.filter((data) => data.CODE == row.DEP_TYPE)[0]) =="undefined")
return "";
return _this.Type_Ary.filter((data) => data.CODE == row.DEP_TYPE)[0].NAME;
return "";
其中:
typeof(_this.Type_Ary.filter((data) => data.CODE == row.DEP_TYPE)[0]) =="undefined"
用来判断它是否为未定义undefined,如果未定义直接返回空,这样就不会报错了。
这个错误本质是由_this.Type_Ary.filter((data) => data.CODE == row.DEP_TYPE)[0].NAME
引起的,如果科室类型和Type_Ary里面的不一样就直接为未定义了,再来个.NAME,肯定直接报错了。
大家的报错可能和我的不一样,本文仅供大家参考,当然最有可能的错误还是:对象没有数据,它为undefined。
TypeError: Cannot read property 'send' of undefined for my commands and a TypeError: Cannot read pro
【中文标题】TypeError: Cannot read property \'send\' of undefined for my commands and a TypeError: Cannot read property \'users\' of undefined for other commands【英文标题】:TypeError: Cannot read property 'send' of undefined for my commands and a TypeError: Cannot read property 'users' of undefined for other commandsTypeError: Cannot read property 'send' of undefined for my commands and a TypeError: Cannot read property 'users' of undefined for other commands 【发布时间】:2021-09-18 11:49:21 【问题描述】:所以我现在使用的代码给了我一个 TypeError: Cannot read property 'send' of undefined for my ping 命令,然后 TypeError: Cannot read property 'users' of undefined for my ban 命令,我还有其他命令由于我做了或忘记做的事情,它不能正常工作,代码一直在工作,直到我向代码添加权限然后一切都走下坡路,我无法弄清楚哪里不工作/它是如何工作的在权限之前,甚至在一段时间之后,然后停止工作,我不知道我是否忘记了要添加的行或忘记了 ;但我很确定在这方面它很好,但任何帮助表示赞赏。
这是我的 main.js
const client = new Discord.Client( partials: ["MESSAGE", "CHANNEL", "REACTION"]);
//const client = require('discord-buttons');
const fs = require('fs');
require('dotenv').config();
//const prefix = '-';
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
['command_handler', 'event_handler'].forEach(handler =>
require(`./handlers/$handler`)(client, Discord);
)
client.login(process.env.DISCORD_TOKEN);
这是我的命令处理程序
const Client = require('discord.js');
const fs = require('fs');
module.exports = (Client, Discord) =>
const command_files = fs.readdirSync(`./commands/`).filter(file => file.endsWith('.js'));
command.execute(client, message, args, cmd, Discord);
for(const file of command_files)
const command = require(`../commands/$file`);
if(command.name)
Client.commands.set(command.name, command);
else
continue;
这是我的事件 handler.js
const Client = require('discord.js');
const fs = require('fs');
module.exports = (Client, Discord) =>
const load_dir = (dirs) =>
const event_files = fs.readdirSync(`./events/$dirs`).filter(file => file.endsWith('.js'));
for(const file of event_files)
const event = require(`../events/$dirs/$file`);
const event_name = file.split('.')[0];
Client.on(event_name, event.bind(null, Discord, Client));
['client', 'guild'].forEach(e => load_dir(e));
这是我的 message.js
const cooldowns = new Map();
module.exports = (Discord, client, message) =>
const prefix = process.env.PREFIX;
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const cmd = args.shift().toLowerCase();
const command = client.commands.get(cmd) || client.commands.find(a => a.aliases && a.aliases.includes(cmd));
if(!command) return;
const validPermissions = [
"CREATE_INSTANT_INVITE",
"KICK_MEMBERS",
"BAN_MEMBERS",
"ADMINISTRATOR",
"MANAGE_CHANNELS",
"MANAGE_GUILD",
"ADD_REACTIONS",
"VIEW_AUDIT_LOG",
"PRIORITY_SPEAKER",
"STREAM",
"VIEW_CHANNEL",
"SEND_MESSAGES",
"SEND_TTS_MESSAGES",
"MANAGE_MESSAGES",
"EMBED_LINKS",
"ATTACH_FILES",
"READ_MESSAGE_HISTORY",
"MENTION_EVERYONE",
"USE_EXTERNAL_EMOJIS",
"VIEW_GUILD_INSIGHTS",
"CONNECT",
"SPEAK",
"MUTE_MEMBERS",
"DEAFEN_MEMBERS",
"MOVE_MEMBERS",
"USE_VAD",
"CHANGE_NICKNAME",
"MANAGE_NICKNAMES",
"MANAGE_ROLES",
"MANAGE_WEBHOOKS",
"MANAGE_EMOJIS",
]
if(command.permissions.length)
let invalidPerms = []
for(const perm of command.permissions)
if(!validPermissions.includes(perm))
return console.log(`Invalid Permissions $perm`);
if(!message.member.hasPermission(perm))
invalidPerms.push(perm);
if (invalidPerms.length)
return message.channel.send(`Missing Permissions: \`$invalidPerms\``);
if(!cooldowns.has(command.name))
cooldowns.set(command.name, new Discord.Collection());
const current_time = Date.now();
const time_stamps = cooldowns.get(command.name);
const cooldown_ammount = (command.cooldown) * 1000;
if(time_stamps.has(message.author.id))
const expiration_time = time_stamps.get(message.author.id) + cooldown_ammount;
if(current_time < expiration_time)
const time_left = (expiration_time - current_time) / 1000;
return message.reply(`Please wait $time_left.toFixed(1) more seconds before using $command.name`)
time_stamps.set(message.author.id, current_time);
setTimeout(() => time_stamps.delete(message.author.id), cooldown_ammount);
try
command.execute(message,args, cmd, client, Discord);
catch (err)
message.reply("There was an error trying to execute this command!");
console.log(err);
这是我的 ping.js
module.exports =
name: 'ping',
cooldown: 10,
permissions: ["SEND_MESSAGES"],
description: "this is a ping command!",
execute(client, message, args, cmd, Discord)
message.channel.send('pong!');
这是我的ban.js
module.exports =
name: 'ban',
aliases: ['b'],
permissions: ["ADMINISTRATOR", "BAN_MEMBERS",],
description: "this is a ban command!",
execute(client, message, args, cmd, Discord)
const member = message.mentions.users.first();
if(member)
const memberTarget = message.guild.members.cache.get(member.id);
memberTarget.ban();
message.channel.send('User has been banned');
else
message.channel.send('Need to mention a member you wish to ban')
【问题讨论】:
【参考方案1】:这是一个非常明显的错误,你给出了错误的定义,你做了一个
command.execute(message,args, cmd, client, Discord);
在你的命令文件中,你已经使用了
execute(client, message, args, cmd, Discord)
您可以通过在命令处理程序中使用以下内容来解决此问题:
command.execute(client, message, args, cmd, Discord);
希望对您有所帮助,如果您需要更多帮助,请在下方评论。
【讨论】:
我在帖子中添加了我的命令处理程序和事件处理程序,我忘记添加了,但是当我添加您的建议时,它并没有解决问题 你能发送确切的错误吗,它给你什么错误? 我把它放在了错误的位置,我的错,但是我已经在我的命令处理程序中拥有了我添加到初始帖子中的代码,现在我在 command_handler.js:7 命令中收到了这个错误。执行(客户端,消息,参数,cmd,不和谐); ^ TypeError: command.execute 不是函数 什么是命令?如果 command 未定义或没有属性:'execute',那么它将失败。 对不起,我不明白你的意思以上是关于TypeError: Cannot read properties of undefined (reading ‘NAME‘)报错解决的主要内容,如果未能解决你的问题,请参考以下文章
TypeError: Cannot read properties of undefined (reading 'app') (Flutter web app)
TypeError: Cannot read properties of undefined (reading 'length') - 想要解释为啥代码这样说
Uncaught (in promise) TypeError: Cannot read properties of null (reading 'fingerprint') Laravel Live
D3.js 一直遇到这个错误 TypeError: Cannot read properties of null (reading 'ownerDocument')?
React Material UI:Appbar 给出了 TypeError: Cannot read properties of undefined (reading '100')
TypeError: Cannot read property 'scrollIntoView' of null 如何解决?