简体   繁体   English

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

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

I am trying to make a warn system with discord.js and I am using Cloud Firestore for the database.我正在尝试使用 discord.js 创建一个警告系统,并且我正在为数据库使用 Cloud Firestore。 I am trying to make a system where when a admin sends a command, it uses the where function to find all documents with the userid of the user the admin is trying to warn.我正在尝试创建一个系统,当管理员发送命令时,它使用where function 查找所有具有管理员试图警告的用户的用户 ID 的文档。

Here is the code that I am using for the command:这是我用于命令的代码:

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")
      }
    }
  }
})

Don't forget that Firebase's .get() method is asynchronous.不要忘记 Firebase 的.get()方法是异步的。 When you use where() to query for all of the documents that meet a certain condition, and use get() to retrieve the results, you need to await for the results.当您使用where()查询所有满足特定条件的文档,并使用get()检索结果时,您需要await结果。 Also make sure that you add async in front of the message handler:还要确保在消息处理程序前面添加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