I want to try some code with firestore emulator before using it in production, I want basically to retrieve a collection documents sort them and set them again in the collection: I have this error while doing a batch delete :
[Error: [firestore/permission-denied] The caller does not have permission to execute the specified operation.]
the code:
useEffect(() => {
(async () => {
await admin_sortUserRanksDB()
})()
}, [])
const admin_sortUserRanksDB = async () => {
const usersData = await admin_getUserDataDBAndClean()
populateUserCollection(usersData)
}
const admin_getUserDataDBAndClean = async () => {
try {
const querySnapshot = await firestore()
.collection('users')
.orderBy('experience_amount', 'desc')
.get();
let rank = 1;
let newDataUsers = [];
for (const user of querySnapshot.docs) {
const userData = user.data();
userData.rank = rank;
newDataUsers.push(userData)
rank++
}
await deleteUserCollection(querySnapshot)
return newDataUsers;
} catch (error) {
if (!__DEV__) {
crashlytics().log(
`error getUserDataDB()
userActions.js ===>> ${error.message}`
);
}
console.log('error getUserDataDB ', error)
return null
}
}
const deleteUserCollection = async (usersQuerySnapshot) => {
// Create a new batch instance
const batch = firestore().batch();
usersQuerySnapshot.forEach(documentSnapshot => {
batch.delete(documentSnapshot.ref);
});
console.log('==============')
return batch.commit();
}
const populateUserCollection = usersData => {
if (usersData) {
const batch = firestore().batch();
usersData.forEach(doc => {
let docRef = firestore()
.collection('users')
.doc(); //automatically generate unique id
batch.set(docRef, doc);
});
batch
.commit()
.catch(error => {
console.log('error populating users', error)
});
}
}
After posting an issue to react-native-firebase
repo i was suggested to modify my rules to be open (only locally) and the batch delete worked.
I used the allow read, write: if true
in firestore.rules file