我正在使用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);
}
答案 0 :(得分:0)
以下是您的代码以及如何处理承诺的一些问题:
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);
}