我在節點上使用 Firebase 的實時資料庫,我發現檢查鍵是否存在的唯一方法是回傳所有資料
{
"salas" : {
"sala01" : {
"activa" : true,
"fechaComienzo" : "2021-10-16 07:09:00Z",
"fechaCreacion" : "2021-10-16 07:09:24Z",
"nombre" : "test",
"masInfo": {
...
}
},
"sala02" : {
"activa" : true,
"fechaComienzo" : "2021-10-16 07:09:00Z",
"fechaCreacion" : "2021-10-16 07:09:24Z",
"nombre" : "test",
"masInfo": {
...
}
}
}
}
async function checkExists(_ref, key) {
const q = query(_ref, ...[orderByKey(), equalTo(key)])
const datos = await get(q);
console.log('@info', q, datos);
return datos.exists();
}
const _ref = ref(getDatabase())
checkExists(_ref, 'sala01')
在上面的例子中,如果我想知道是否sala01存在,它會為我帶來每個節點及其后代。
我想知道是否有任何其他方法可以簡單地檢查是否sala01存在而不帶我整個節點,或者是否有一些技巧可以帶一個深度的節點,就像我在 REST API 檔案中看到的那樣,淺
uj5u.com熱心網友回復:
這種查詢比需要的更復雜:
const q = query(_ref, ...[orderByKey(), equalTo(key)])
如果您已經知道節點的鍵,則不需要使用查詢,但可以直接訪問該鍵。
async function checkExists(_ref, key) {
const ref = child(_ref, key)
const datos = await get(ref);
return datos.exists();
}
這不會讀讀整個/_ref節點,但將讀取整個/_ref/$key節點。如果不讀取該路徑,則無法測驗該路徑是否存在。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/369207.html
