亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Firestore“where”查詢未按預期工作

Firestore“where”查詢未按預期工作

慕雪6442864 2023-06-09 10:40:36
我正在嘗試從我的 firestore 數據庫中進行一個簡單的查詢,但我遺漏了一些非常明顯的東西。我嘗試在線查找,但沒有任何效果。對于某些背景,我有一個“cf”集合,我試圖在其中查詢“hsc”值等于“1”的對象,但我沒有得到任何回報。exports.getOneTodo = (request, response) => {    db        .collection('cf')        .where("hsc", "==", "1")         .get()        .then((doc) => {            if (!doc.exists) {                return response.status(404).json(                    {                         error: 'Todo not found'                     });            }            TodoData = doc.data();            TodoData.todoId = doc.id;            return response.json(TodoData);        })        .catch((err) => {            console.error(err);            return response.status(500).json({ error: error.code });        });};以下是 Firestore 規則。rules_version = '2';service cloud.firestore {  match /databases/{database}/documents {    match /{document=**} {      allow read, write    }  }}我正在通過郵遞員對此進行測試。我已經嘗試將 firebase 規則更改為適用于任何事物,但似乎仍然沒有任何效果。更新:以下是我初始化數據庫的方式const admin = require('firebase-admin');admin.initializeApp();const db = admin.firestore();module.exports = { admin, db };
查看完整描述

2 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

您的代碼需要一個文檔,但必須為查詢返回多個文檔做好準備。當您在 Query 對象上運行時get(),它將產生一個QuerySnapshot對象。正如您從 API 文檔中看到的那樣,它沒有屬性exists。該屬性的檢查將始終為“假”。您要做的是檢查結果,首先查看是否有任何文檔,然后獲取第一個:

db

? ? ? ? .collection('cf')

? ? ? ? .where("hsc", "==", "1")?

? ? ? ? .get()

? ? ? ? .then((qsnapshot) => {

? ? ? ? ? ? if (qsnapshot.docs.length > 0) {

? ? ? ? ? ? ? ? const dsnapshot = qsnapshot.docs[0];

? ? ? ? ? ? ? ? // send the response using dsnapshot.data()

? ? ? ? ? ? }

? ? ? ? ? ? else {

? ? ? ? ? ? ? ? // send the response saying nothing was found

? ? ? ? ? ? }

? ? ? ? })


查看完整回答
反對 回復 2023-06-09
?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

現在你需要使用

  db.collection("id").whereGreaterThan("field","value")
  .whereEqualTo("field","value")
  .whereLessThen("field","value")


查看完整回答
反對 回復 2023-06-09
  • 2 回答
  • 0 關注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號