我是一個使用Firebase實時資料庫的新手,我無法洗掉指定id的產品鍵子。
我試過這種方法,但沒有任何收獲:
我試過這種方法,但沒有任何收獲。
removeProductById = async(id, user)=> {
const userProductRef = db.ref(`products/product`) 。
try{
var query = userProductRef.orderByChild('id').equalTo(id)。
query.remove()
})
}catch(error){
console.log(`The error has occurred ${error.message}`)
}
}
所以我想知道如何才能通過指定的值來洗掉任何鍵的ipe。事先謝謝你,你一直在幫助我。
uj5u.com熱心網友回復:
這里有兩個問題:
你的查詢與你想洗掉的節點不匹配
。你的查詢從一個節點products/product開始,這個節點在你的資料庫中并不存在,然后對每個子節點的id屬性進行排序/過濾。
你想要的是,從products節點開始,然后對其product/id屬性的每個子節點進行排序/過濾:
在代碼中:
const userProductRef = db.ref(`products`)。
var query = userProductRef.orderByChild('product/id').equalTo(id)。
別忘了,你需要在你的規則中的products層為product/id定義一個索引。
Firebase不支持delete-queries
。你不能簡單地把查詢和洗掉指令傳遞給Firebase。要在資料庫中寫入一個節點,你需要知道該節點的整個路徑。
所以你需要:
- 執行一個查詢。
- 在您的應用程式代碼中回圈查看結果。
- 單獨或在多路徑更新中洗掉每個匹配節點。
通過上面的query,這將是:
query.once("value"/span>)。 then((snapshot) => {
snapshot.forEach((child) =>/span> {
child.ref.remove()。
});
})
或者用多路徑/批量更新:
query.once("value") 。 then((snapshot) =>/span> {
let updates = {};
snapshot.forEach((child) =>/span> {
updates[child.key] = null;
});
ref.update(uplets)。
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331531.html
標籤:
下一篇:排除回應json檔案中的資料成員

