慕雪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
? ? ? ? ? ? }
? ? ? ? })

HUWWW
TA貢獻1874條經驗 獲得超12個贊
現在你需要使用
db.collection("id").whereGreaterThan("field","value") .whereEqualTo("field","value") .whereLessThen("field","value")
添加回答
舉報
0/150
提交
取消