Javascript 如何修复无法读取属性的错误';未定义的修剪

Javascript 如何修复无法读取属性的错误';未定义的修剪,javascript,discord,discord.js,Javascript,Discord,Discord.js,代码: 错误: case `prefix`: var prfx = args[3]; if (!prefix) return msg.reply(`prefix ?`); data.prefix = prfx.trim(); msg.channel.send(`done , my prefix now is : ${prfx}`); break; 在您的情况下,prfx未定义,但您正在检查一个变量前缀,并且仅当该变量为false时返回。您必须同时或专门检查prf

代码:

错误:

case `prefix`:
    var prfx = args[3];
    if (!prefix) return msg.reply(`prefix ?`);
    data.prefix = prfx.trim();
    msg.channel.send(`done , my prefix now is : ${prfx}`);
break;

在您的情况下,
prfx
未定义,但您正在检查一个变量
前缀
,并且仅当该变量为false时返回。您必须同时或专门检查
prfx

TypeError: Cannot read property 'trim' of undefined at Client.client.on.msg

args[3]在args数组中为null或未定义。修剪前你应该检查一下

case `prefix`:
   var prfx = args[3];
   if (!prefix || !prfx) { // Check for prfx as well, since that one could be undefined, maybe !prefix is not even needed or just mispelled
      return msg.reply(`prefix ?`);
   }
   data.prefix = prfx.trim();
   msg.channel.send(`done , my prefix now is : ${prfx}`);
   break;
 case `prefix`:
   var prfx = args[3];
   if (!prfx) { // prfx is not null or undefined?
     throw 'prfx is null or undefined'; // throw an exception
   }
   data.prefix = prfx.trim();
   msg.channel.send(`done , my prefix now is : ${prfx}`);
   break;