简体   繁体   中英

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:

    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. (thanks to Arun for pointing that out!)

The channel.setParent call fails on this line since category is undefined . Looks like the guild does not have a category channel named '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.

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. I think it should be permissionOverwrites . Check the docs .


This is unrelated to the question but I would recommend renaming category to categoryChannel for clarity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM