簡體   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