简体   繁体   中英

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. 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.

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. 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. Also make sure that you add async in front of the message handler:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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