NodeJS 我想删除quick.db中的某些内容

new9mtju  于 2023-03-22  发布在  Node.js
关注(0)|答案(1)|浏览(115)

我试图使用discord.js制作一个discord bot,作为db,我使用quick.db,我想删除一个项目,但当我使用我的代码时,它删除了整个数组,但我想删除一个东西

const db = quick.db

const emote = require("../../config/emotes.json");

const { MessageEmbed } = require("discord.js");

module.exports = {

    name: 'delete',

    run: async(client, message, args) => {

       db.delete(`item_${message.author.id}`, 'ThiefOutfit')

const embed = new MessageEmbed()

.setDescription("You deleted Thief Outfit")

message.channel.send(embed)

}

}
az31mfrm

az31mfrm1#

这是发生在你身上,因为你删除了与整个数组相关联的键,这就是为什么

根据文档,有一个如何处理数组值的选项。下面是文档中的代码

await db.pull("myUser.items", "Health Potion"); // Returns -> { balance: 700, items: [ 'Sword', 'Shield', 'Armor' ] }```
Here another way to handle this:
 1. First you will get the array associated with key
 2. Then filter the array, and remove which one you no need
 3. Then again set new filtered array with the same key.
```let currentArray = await db.get("yourkey")  //[1, 2, 3]
const newArray = currentArray.filter((e) => e == 1)
await db.set("yourkey", newArray)```

相关问题