[英]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.