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

oo7oh9g9  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(78)

我正在尝试编写一个Discord.JS机器人,它使用MongoDB的Model and Schema功能列出并删除特定的通道/线程。我已经弄清楚了所有其他的事情,实际的消息删除、通道删除,以及删除功能所需的所有其他东西,但是由于某种原因,提示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;

这只是我围绕这个功能构建的斜杠命令所使用的switch case中的片段,也是删除清单的case。
它不会在控制台中导致任何错误,但是当我检查数据库时,我放置的测试列表仍然存在,似乎没有消失。
我做错了什么吗?无论我怎么看,似乎都找不到任何能解决这个问题的东西。它不工作的原因是因为它被列在ChatListing.findOne()函数中吗?如果是这样,我该如何修改它,使其在该函数之外工作,同时仍然保留删除功能?

vxf3dgd4

vxf3dgd41#

尝试使用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);
  });

相关问题