This question already has answers here:
Deleting all documents in Firestore collection
(16 answers)
How to delete all the documents in a firestore collection database
(1 answer)
Closed 27 days ago.
i recently began to use firebase and i ran into a problem, to get it straight, i want to make a e-commerce site, ofcourse it has cart, i used firebase for my back-end, for every user, i create a document in "users" collection, each user's document id is the uid of that given user. On every document, i have a cart, and in the cart, the products, on my cart page, i want to have a "Empty Cart" that deletes all the products, meaning it deletes "cart" collection. How can i do that? I have left some photos below that will probably explain the problem better.
Image of the firebase tree
I tried this and it did not work!
const deleteCart = async () => {
const cartDoc = `users/${user.uid}/cart`
await deleteDoc(db, cartDoc)
}
i get this error : " TypeError: Cannot use 'in' operator to search for '_delegate' in undefined "
Related
This question already has answers here:
Query based on multiple where clauses in Firebase
(8 answers)
How to do the following query in Firebase? (more than one where condition) [duplicate]
(1 answer)
NoSQL database design for queries with multiple restrictions (Firebase)
(1 answer)
Closed 5 months ago.
useEffect(() => {
fireDb
.child(`All Ride Requests`)
.orderByChild(`car_model`)
.equalTo("Volvo")
// .orderByValue("car_plaka")
// .equalTo("S40")
.on("value", (snapshot) => {
if (snapshot.val() !== null) {
setCar({ ...snapshot.val() });
} else {
setCar({});
}
});
return () => {
setCar({});
};
}, []);
This way I can't assign 2 queries. Is there a way to do this? Is there a way to query data in the same directory?
Unfortunately, the Realtime database does not support queries on multiple properties, supports only queries on a single child property. So something like this is not possible:
fireDb
.child(`All Ride Requests`)
.orderByChild(`car_model`)
.equalTo("Volvo")
.orderByChild(`car_plaka`)
.equalTo("Value")
You can however create a new field which in your database that should look like this:
Firebase-root
|
--- All Ride Requests
|
--- carModel: "Volvo"
|
--- carPlaka: "Value"
|
--- carModel_carPlaka: "Volvo_Value"
So as you see, the carModel_carPlaka property combines the values that you want to filter on. In code should look like this:
fireDb
.child(`All Ride Requests`)
.orderByChild(`carModel_carPlaka`)
.equalTo("Volvo_Value")
Unlike the Realtime Database, Cloud Firestore allows compound queries. You should take a look at this. So a query like the one below is allowed in Firestore without creating a combined property.
allRideRequestsRef.where('carModel', '==', 'Volvo').where('carPlaka', '==', 'Value');
I have deployed an app with React and I am using Firebase Realtime database to store some info about attention tickets in a call center. The database will store aprox 80 tickets info per day, but this is cumulative. I want to avoid this so I will not reach the firebase storage limit.
My idea so far is to delete every day at noon the tickets from the database, so It will only store the data from the current date and eliminate it at noon.
I am using remove() function from firebase, but when I tried referencing to the collection, It was entired deleted, I just want to delete the documents but not the entire collection.
Is there a way to specify Firebase to delete docs only, maybe to delete every docs except one?
This is the bunch of code that I pretend to use for deleting (Based on JS)
function deletedata(){
const dbRef = query(ref(db,'/mycollectionpath'));
onValue(dbRef, (snapshot)=>{
snapshot.forEach(childSnapshot=>{
let keyName = childSnapshot.key;
remove(dbRef);
})
});
}
setInterval(deletedata,1000)
The Firebase Realtime Database automatically creates parent nodes when you write a value under them, and it automatically deletes parent nodes when there are no longer any values under them. There is no way to have a node without a value.
If you want to keep the node, consider writing a dummy value. For example, this would remove all child nodes from mycollectionpath and replace them with a single true value:
const dbRef = ref(db, '/mycollectionpath');
dbRef.set(true);
This question already has answers here:
Delete in cloud firestore gives "doc.delete is not a function"
(2 answers)
Closed 1 year ago.
I have an app where a user can create a post. Users can also bookmark these posts. However, when a post author deletes his post that others may have already bookmarked, I need to also automatically delete those bookmarks that are associated to the deleted post.
Here's my data model (when a user bookmarks a post, the bookmark is saved to the user's record in a subcollection called bookmarks:
I've written the code logic to delete a bookmark like so:
const postsQuery = await this.$fire.firestore
.collectionGroup('bookmarks')
.where('id', '==', this.post.id)
.get()
postsQuery.forEach((doc) => doc.delete())
console.log('bookmark deleted')
} catch (error) {
console.error('error deleteing post.', error)
}
Problem: I get the following console error when trying to delete the bookmark:
EditPost.vue?4144:107 error deleteing post. TypeError: doc.delete is not a function
at eval (EditPost.vue?4144:95)
at eval (prebuilt-28889c43-f8ea673b.js?f270:18228)
at eval (prebuilt-28889c43-f8ea673b.js?f270:16453)
at eval (prebuilt-28889c43-f8ea673b.js?f270:11563)
at t.inorderTraversal (prebuilt-28889c43-f8ea673b.js?f270:2801)
at t.inorderTraversal (prebuilt-28889c43-f8ea673b.js?f270:2719)
at t.forEach (prebuilt-28889c43-f8ea673b.js?f270:11562)
at t.forEach (prebuilt-28889c43-f8ea673b.js?f270:16452)
at t.forEach (prebuilt-28889c43-f8ea673b.js?f270:18227)
at _callee$ (EditPost.vue?4144:94)
How can I correctly write this delete logic? Thanks!
doc.delete is not a function
This is because doc is a QueryDocumentSnapshot and not a DocumentReference. You need to use the ref property as follows:
const postsQuery = await this.$fire.firestore
.collectionGroup('bookmarks')
.where('id', '==', this.post.id)
.get()
postsQuery.forEach((doc) => doc.ref.delete())
Note that if you want to monitor when all the bookmarks are deleted (console.log('bookmark deleted')) you may use a batched write (a batched deletion in your case!) or Promise.all()
This question already has an answer here:
Firebase realtime database validation username and email
(1 answer)
Closed 2 years ago.
This is my database structure for the Users.
As you can see in the picture, there are 2 same Identification number are registered successfully.
How can I prevent the "ICNumber"(Identification Number) to be registered more than once?
This is the rule in my RealTime Database:
I tried putting this piece of code but it doesn't seem to work. Is there any way I can do the prevention in my Javascript or any correct way in my Firebase Rule?
In your code, you can do the following:
// Get a reference to the database service
var database = firebase.database();
var dataRetrieved = database.ref('users').child(userId).orderByChild("ICNumber").equalTo("000326141809");
dataRetrieved.on('value', function(snapshot) {
if(snapshot.exists()){
// do the required
}
});
You can use a query to check if you have an ICNumber equal to 000326141809 and then use the method exists() to check if it is already in the database
This question already has answers here:
Firestore: get document back after adding it / updating it without additional network calls
(4 answers)
Closed 4 years ago.
I'm trying to get back some info about the document I just created/updated without making a second request.
I know that I get back a Document reference with the Id of the document, but I need additional info like createTime and updateTime.
Currently, this is the way I'm trying to achieve that, but I would like to spare that extra request since it's not very efficient.
const docRef = await database.collection('tagsCollection').add({
...input
createTime: firestore.FieldValue.serverTimestamp()
});
const doc = await docRef.get();
return {
...doc.data(),
id: doc.id,
createTime: doc.data().createTime.toMillis().toString()
};
Since those fields come from Firestore on the server side, it's safe to assume that the client doesn't know what they are until a DocumentSnapshot has been queried. You could perhaps make a guess on your own, but you're essentially trying to guess what the round trip latency is for your call to add().