繁体   English   中英

为什么 Cloud Firestore 的 where function 不起作用

[英]Why is the where function of Cloud Firestore not working

我正在尝试使用 discord.js 创建一个警告系统,并且我正在为数据库使用 Cloud Firestore。 我正在尝试创建一个系统,当管理员发送命令时,它使用where function 查找所有具有管理员试图警告的用户的用户 ID 的文档。

这是我用于命令的代码:

client.on('message', msg => {
  if (msg.author.id != client.user.id) {
    if (msg.content.startsWith(";getwarns")) {
      let user = msg.mentions.users.first()
      if (user) {
        let member = msg.guild.member(user)
        if (member) {
         let warnref = db.collection('Warns')
         console.log(user.id)
         let snapshot =  warnref.where('UserId', '==', user.id).get()
         console.log(snapshot)
        }
      } else {
       msg.reply("You have not mentioned anybody")
      }
    }
  }
})

不要忘记 Firebase 的.get()方法是异步的。 当您使用where()查询所有满足特定条件的文档,并使用get()检索结果时,您需要await结果。 还要确保在消息处理程序前面添加async

client.on('message', async (msg) => {
  if (msg.author.id != client.user.id) {
    if (msg.content.startsWith(";getwarns")) {
      let user = msg.mentions.users.first()
      if (user) {
        let member = msg.guild.member(user)
        if (member) {
         let warnref = db.collection('Warns')
         console.log(user.id)
         let snapshot =  await warnref.where('UserId', '==', user.id).get()
         console.log(snapshot)
        }
      } else {
       msg.reply("You have not mentioned anybody")
      }
    }
  }
})

暂无
暂无

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

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