如何检查quick.db中是否存在特定的东西?

问题描述

所以我正在制作一个不和谐的游戏机器人,你可以通过打开箱子来收集字符,但问题是当你已经拥有它们时,这些字符会再次出现。

我使用了方法 db.push

代码

if(has === true){
  console.log(has)
  let embed = new discord.MessageEmbed()
.setTitle('Chest opening | Clash Chest')
.setDescription("<@"+message.author+"> got:\n"+amount+" :coin:")
.setColor('RANDOM')
.setFooter('Created by Tahmid GS#1867')
 message.channel.send(embed)
  await db.fetch(`coin_${message.author.id}`)
  db.add(`coin_${message.author.id}`,amount)
}
else if(has === false){
  console.log(has)
await db.fetch(`troop_${message.author.id}`)
db.push(`troop_${message.author.id}`,nada)



let embed = new discord.MessageEmbed()
.setTitle('Chest opening | Clash Chest')
.setDescription("<@"+message.author+"> got:\n"+amount+" :coin:\n||Common: "+nada+"||")
.setColor('RANDOM')
.setFooter('Created by Tahmid GS#1867')
  
  message.channel.send(embed)

   await db.fetch(`coin_${message.author.id}`)
  db.add(`coin_${message.author.id}`,amount)
  await db.fetch(`card_${message.author.id}`)
  db.add(`card_${message.author.id}`,1)
}

let has = db.has(troop_${message.author.id},nada) 我用过 let has = db.has(troop_${message.author.id}.nada) 并让 has = db.has(nada,troop_${message.author.id})

但是好像不行,在控制台中是“false”

nada 是随机字符

解决方法

您正在检查数据库是否具有特定键。在键 troop_${message.author.id} 下的数据库中有一个数组。您想检查该数组是否包含字符。

let has = (db.get(`troop_${message.author.id}`) || []).includes(nada);

此外,您可能还想检查 db.get() 返回 undefined 或类似内容的可能性。在这种情况下,我们对空数组调用 .includes()。而不是收到错误TypeError:无法读取未定义的属性“包含”