繁体   English   中英

MongoDB findOneAndDelete() 不会删除指定的查询——我不太明白为什么?

[英]MongoDB findOneAndDelete() will not delete the specified query-- I can't quite figure out why?

我正在尝试编写一个 Discord.JS 机器人,它使用 MongoDB 的 Model 和架构功能列出和删除特定的频道/线程。 我已经弄清楚了其他所有内容,实际的消息删除、频道删除以及删除 function 所需的所有其他内容,但出于某种原因提示 MongoDB 使用指定的 ID 删除 ChatListing 架构不起作用。

            case 'remove':

                modal.setTitle('Remove a Listing');

                const listingIDInput = new TextInputBuilder()
                    .setCustomId('listingIDInput')
                    .setLabel(`What's the ID of your listing?`)
                    .setPlaceholder('EX... 14309')
                    .setMinLength(5)
                    .setStyle(TextInputStyle.Short)
                    .setRequired(true);

                const rmrow = new ActionRowBuilder().addComponents(listingIDInput);

                modal.addComponents(rmrow);
                await interaction.showModal(modal);

                try {
                    await interaction.awaitModalSubmit({ time: 120_000 }).then( (interaction) => {

                        const listingToRemove = interaction.fields.getTextInputValue('listingIDInput');

                        ChatListing.findOne({ GuildID: guild.id, ListingID: listingToRemove }, async(err, data) =>{
                            if(err) throw err;
                            if(!data) return;

                            if(data.MemberID == member.id) {

                                const channel = await guild.channels.cache.get(data.Channel);
                                const msg = await channel.messages.fetch(data.MessageID);
                                msg.delete();

                                var id = data._id;

                                ChatListing.findByIdAndDelete({_id: mongoose.Types.ObjectId(id)});

                                embed.setTitle('Listing successfully removed.')
                                    .setColor('Green')
                                    .setDescription('⚠️ | Your chat listing has been removed successufully. We\'re sorry to see it go! | ⚠️')
                                    .setTimestamp();

                                await interaction.reply({ embeds: [embed], ephemeral: true });

                            } else {
                                embed.setTitle('You aren\'t the owner of the listing!')
                                    .setColor('Red')
                                    .setDescription('You aren\'t capable of removing this listing because you aren\'t the user that posted it.')
                                    .setTimestamp();

                                await interaction.reply({ embeds: [embed], ephemeral: true });
                            }
                        });
                    });
                } catch (err) {
                    console.error(err);
                }

            break;

这只是我围绕此功能构建的用于斜杠命令的开关案例中的片段,以及用于删除列表的案例。

它不会在控制台中导致任何错误,但是当我检查数据库时,我放置的测试列表仍然存在并且似乎不是 go。

我做错了什么吗? 无论我在哪里看,我似乎都找不到任何能为我解决这个问题的东西。 它不工作的原因是因为它列在 ChatListing.findOne() function 中吗? 如果是这样,我如何修改它以在 function 之外工作并仍然保留删除功能?

尝试使用findOneAndDelete并使用返回的 Promise 来处理成功或失败:

ChatListing.findOneAndDelete({_id: mongoose.Types.ObjectId(id)})
  .then(() => {
    embed.setTitle('Listing successfully removed.')
      .setColor('Green')
      .setDescription('⚠️ | Your chat listing has been removed successufully. We\'re sorry to see it go! | ⚠️')
      .setTimestamp();

    interaction.reply({ embeds: [embed], ephemeral: true });
  })
  .catch(error => {
    console.error(error);
  });

暂无
暂无

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

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