我正在嘗試洗掉 firebase 檔案,但問題是我想使用欄位洗掉特定檔案。

如上所示,我在許多檔案中都有 user_uid_1 和 user_uid_2。我想匹配它們,就像我單擊洗掉時應該洗掉每個帶有(401 和 337)的檔案一樣。
export const deleteChat = (chatId) => {
return async (dispatch) => {
const db = firestore();
db.collection("conversations")
.doc(chatId)
.delete()
.then(() => {
dispatch({
type: userConstants.GET_REALTIME_MESSAGES,
});
})
.catch((error) => {
console.log(error);
});
};
};
uj5u.com熱心網友回復:
您可以使用該where方法查詢并為找到的每個檔案回圈該delete()方法。請參閱下面的示例代碼:
const coversations = db.collection('conversations')
.where('user_id_1', '==', '401')
.where('user_id_2', '==', '337');
coversations.get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
doc.ref.delete();
});
});
如果(401 and 337)可以同時出現user_id_1和user_id_2,您可以做一個簡單的邏輯來檢查該欄位是否出現。請參閱下面的示例代碼:
const coversations = db.collection('conversations');
coversations.get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
const n = ['401', '307'];
if (n.includes(doc.data().user_uid_1) && n.includes(doc.data().user_uid_2)) {
doc.ref.delete();
}
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/453351.html
