簡體   English   中英

如何修復此類型錯誤:無法讀取 null 的屬性“名稱”

[英]how to fix this TypeError: Cannot read property 'name' of null

如何修復此錯誤

music.on('voiceStateUpdate',(lama, baru) => {
var state = null;  
let Role = baru.roles.find((r) => ["IRON", "BRONZE"].includes(r.name));
  const kategorikanal = '700743802574602260'
  const channelid = '700743824346972231'
  if(!lama.voiceChannel && !baru.voiceChannel) return;
  if(!lama.voiceChannel && baru.voiceChannel) {state = "join"}
  else if(lama.voiceChannel && !baru.voiceChannel) {state = "leave"}
  else if(lama.voiceChannel.id !== baru.voiceChannel.id) {state = "move"}
  else if(lama.voiceChannel.id == baru.voiceChannel.id) return;
  console.log(state);
//!baru.member.roles.has(allowedRole)
  if(baru.voiceChannelID === channelid || !baru.voiceChannelID === Role || Role !== null && Role !== '') {
console.log(baru.displayName + ' gabisabgo hrus ada rank ranked ');
   // const Role = baru.guild.roles.get("724997095236304987");
    baru.guild
    .createChannel(`${Role.name} | ${baru.user.username}`,"voice")
    .then(tempChannel => {
        tempChannel.overwritePermissions(baru.guild.defaultRole.id, {
   CONNECT: false,
})

    tempChannel.overwritePermissions(Role.id, {
        CONNECT: true
    })
      tempChannel.setParent(kategorikanal);
      baru.setVoiceChannel(tempChannel.id);
      tempChannel.setUserLimit("5");
      })
      
  .catch(console.error)
      
}
  if(lama.voiceChannelID || !lama.voiceChannelID === Role || Role !== null && Role !== '') {     
      console.log(lama.displayName + ' gabisabgo hrus ada rank ranked ');

      const voicelama = lama.guild.channels.get(lama.voiceChannelID);

    let Role = baru.roles.find((r) => ["IRON", "BRONZE"].includes(r.name));
      if(voicelama.name.startsWith(`${Role.name} | ${baru.user.username}`)){
      let sawadikap = `**${baru.user.username}'s**` + " **Team**"
      var koko = new Discord.RichEmbed()
      .setColor("#FF4654")
      .setThumbnail(`${baru.user.avatarURL}`)
      .addField('**Good Game Well Played**',`${sawadikap}`)
      .setFooter("@Valorant Indonesia Community." , 'https://i.imgur.com/yPWqxxu.png') 
       voicelama.delete()
      .then(function() {
        music.channels.get('725080861392896101').send(koko)
  })
      .catch(console.error);
  }
    }
  })

錯誤視圖

.createChannel(${Role.name} | ${baru.user.username},"voice")
 ^  TypeError: Cannot read property 'name' of null 

您是否在調試模式下單步執行了代碼? 我建議設置斷點、創建監視並在您逐步執行時檢查變量的值。 如果您覺得這樣做不舒服,您能否輸入以下內容,並告訴我控制台記錄的內容?

console.log(Role)
console.log(Role.name)

雖然Role不是null ,但null Role.name這意味着它沒有分配任何值。 該問題發生在這里:

let Role = baru.roles.find((r) => ["IRON", "BRONZE"].includes(r.name));

所以我看到了兩種可能性:

  1. 沒有角色包含這些名稱。
  2. 我認為find應該只產生一個結果,但我似乎找不到該方法的良好文檔。 是否有可能找到兩個角色並返回一個集合? 這意味着會有多個角色的集合,這意味着角色不會包含角色 object 類型所包含的數據成員。 這意味着您必須在使用名稱之前索引其中一個角色。

//

//if there isn't a matching role, then terminate the method.
if (Role == null)
{
    return;
}
//if there are multiple roles that match the criterion, just use the first one. 
//The alternative is that we could make it into a loop that handles it for all of them.
else if (Role instanceof Array)
{
    if (Role.length == 0)
    {
        return;
    }

    Role = Role[0]
}

在調用baru.guild.createChannel之前添加以上行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM