简体   繁体   English

如何修复无法读取未定义的属性“hasOwnProperty”?

[英]How to fix Cannot read property 'hasOwnProperty' of undefined?

My code checks if a category exists, and if it doesn't exist, it creates it, and after that, creates a channel and moves it a category, but it created both and gave me this error: TypeError: Cannot read property 'hasOwnProperty' of undefined without moving the channel in the category This is the code to the error:我的代码检查一个类别是否存在,如果它不存在,则创建它,然后创建一个通道并将其移动一个类别,但它创建了两者并给了我这个错误: TypeError: Cannot read property 'hasOwnProperty' of undefined未移动类别中的通道这是错误的代码:

    const server = message.guild;
    const channel = await server.channels.create(`ticket: ${message.author.tag}`);
    let category = server.channels.cache.find(c => c.name == "Tickets" && c.type == "category")
    if (!category) {
   type: 'category',
   updateOverwrite: [server.id, {
    deny: ['VIEW_CHANNEL'],
   }]
   })
  .catch(console.error);
    }


    channel.setParent(category);

Edit: The problem was that I forgot to reassign the category after I created it, also using updateOverwrite is wrong, you should use permissionOverwrites instead.编辑:问题是我在创建类别后忘记重新分配类别,使用updateOverwrite也是错误的,您应该改用permissionOverwrites (thanks to Arun for pointing that out!) (感谢 Arun 指出这一点!)

The channel.setParent call fails on this line since category is undefined .由于category undefined因此该行的channel.setParent调用失败。 Looks like the guild does not have a category channel named 'Tickets' .公会好像没有名为'Tickets'的分类频道。

Since you're creating the channel if it doesn't exist, you should await it and reassign category to the newly created channel to use in the channel.setParent call.由于您正在创建不存在的频道,因此您应该await它并将category重新分配给新创建的频道以在channel.setParent调用中使用。

if (!category) {
  category = await server.channels.create('Tickets', {
    type: 'category',
    // ...
  })
}

channel.setParent(category)

Btw, it looks like updateOverwrite is not a valid option in the server.channels.create call.顺便说一句,看起来updateOverwrite不是server.channels.create调用中的有效选项。 I think it should be permissionOverwrites .我认为应该是permissionOverwrites Check the docs .检查文档


This is unrelated to the question but I would recommend renaming category to categoryChannel for clarity.这与问题无关,但为了清楚起见,我建议将categoryChannel category

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

相关问题 如何修复无法读取未定义的属性 hasOwnProperty - How to fix Cannot read property hasOwnProperty of undefined 未捕获的类型错误:无法读取未定义的属性“hasOwnProperty” - Uncaught TypeError: Cannot read property 'hasOwnProperty' of undefined Extjs 搜索字段无法读取未定义的属性“hasOwnProperty” - Extjs searchfield Cannot read property 'hasOwnProperty' of undefined 类型错误:无法读取未定义的属性“hasOwnProperty” - TypeError: Cannot read property 'hasOwnProperty' of undefined 灰烬数据保存:TypeError:无法读取未定义的属性“ hasOwnProperty” - Ember Data Save: TypeError: Cannot read property 'hasOwnProperty' of undefined 即使在hasOwnProperty检查之后,“无法读取未定义的属性” - “Cannot read property of undefined” even after hasOwnProperty check OverlayManager错误。 未捕获的TypeError:无法读取未定义的属性'hasOwnProperty' - OverlayManager Error. Uncaught TypeError: Cannot read property 'hasOwnProperty' of undefined Carto 引发 TypeError:无法读取未定义的属性“hasOwnProperty” - Carto raises TypeError: Cannot read property 'hasOwnProperty' of undefined 如何修复“无法读取未定义的属性”? - how to fix 'Cannot read property of undefined'? 如何修复“无法读取未定义的属性'get'” - How to fix “Cannot read property 'get' of undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM