[英]Deleting data on RealTime Database - Firebase
I'm using firebase realtime database and has following rules setup for the database, 我正在使用Firebase实时数据库,并为数据库设置了以下规则,
{
"rules": {
".read": "auth !== null",
".write": "auth !== null"
}
}
But when I try to delete an entry, it gives me an error saying permission denied as follows. 但是,当我尝试删除一个条目时,它给我一个错误,表明权限被拒绝,如下所示。
Database: Client doesn't have permission to access the desired data. (database/permission-denied).
What should I do? 我该怎么办? I can't understand why I can do all the reads and writes pretty oki with the current rules and not delete.
我不明白为什么我可以使用当前规则进行所有读写操作,而不删除。
Can anyone help me with this? 谁能帮我这个?
Note: Image of the results related to the Firebase Simulator 注意:与Firebase Simulator相关的结果图像
When checked for the firebase.auth().currentUser
: 当检查
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);
}
Here are some issues with your code and how you are handling 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);
}
Here's a fixed version: 这是固定版本:
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
}
Here is a version without the extra promises
array. 这是一个没有多余的
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.