簡體   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