[英]Deleting data on RealTime Database - Firebase
我正在使用Firebase實時數據庫,並為數據庫設置了以下規則,
{
"rules": {
".read": "auth !== null",
".write": "auth !== null"
}
}
但是,當我嘗試刪除一個條目時,它給我一個錯誤,表明權限被拒絕,如下所示。
Database: Client doesn't have permission to access the desired data. (database/permission-denied).
我該怎么辦? 我不明白為什么我可以使用當前規則進行所有讀寫操作,而不刪除。
誰能幫我這個?
注意:與Firebase Simulator相關的結果圖像
當檢查firebase.auth().currentUser
:
deleteUserAccount(userId) {
let knownLocationRef = this.database.ref('lastKnown/' + userId);
let promises = [];
console.log('auth details', this.auth.currentUser);
console.log('auth details null', this.auth.currentUser !== null); //returns true
knownLocationRef.once('value').then( (data) => {
console.log('data ', data.val());
if (data.val() !== null) {
let prevLat = data.val().lat;
let prevLong = data.val().long;
console.log('auth details', this.auth.currentUser);
console.log('auth details null', this.auth.currentUser !== null); //returns false
promises.push(knownLocationRef.remove());
}
});
return Promise.all(promises);
}
以下是代碼的一些問題以及如何處理promise:
function deleteUserAccount(userId) {
let knownLocationRef = this.database.ref('lastKnown/' + userId);
let promises = []; // good idea for collecting a list of distinct
// promises, maybe not here
// This is a promise, 'then' is a continuation from which you can return
// either a data result or another promise
knownLocationRef.once('value').then((data) => {
// if data is valid, return another promise
if (data.val() !== null) {
// ...
// If you were looping and collecting promises, then this might help,
// but you don't want to collect inner promises.
promises.push(knownLocationRef.remove());
}
});
// This returns before everything in `.then` executes
return Promise.all(promises);
}
這是固定版本:
function deleteUserAccount(userId) {
let knownLocationRef = this.database.ref('lastKnown/' + userId);
let promises = []; // For the sake of argument, I will use this to collect
// outer promises.
// normally I would return here, but as an example, I'll uses the
// promises array
promises.push(knownLocationRef.once('value').then((data) => {
// if data is valid, return another promise
if (data.val() !== null) {
// ...
// Return a Promise from a promise
return knownLocationRef.remove();
}
return null; // no data to process
}));
// wait on outer promises
return Promise.all(promises)
.catch(console.log); // log errors from any promise
}
這是一個沒有多余的promises
數組的版本。
function deleteUserAccount(userId) {
let knownLocationRef = this.database.ref('lastKnown/' + userId);
return knownLocationRef.once('value')
.then((data) => {
// if data is valid, return another promise
if (data.val() !== null) {
// Return a Promise from a promise
return knownLocationRef.remove();
}
return null; // no data to process
})
.catch(console.log);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.