How can i update an item in an array on firebase [duplicate] - javascript

This question already has answers here:
Firestore Update single item in an array field
(2 answers)
How can I update an object inside of an array in firestore?
(1 answer)
Closed 6 months ago.
I'm trying to make a button that can edit an item within a documents array in firebase. Basically, the functionality I'm building is: you click an EDIT button, and it changes a tag into an tag where you would make your edit/update to the array item. Then you would click a DONE button and the update to the array would happen. For some reason, I can only find documentation on firebase for updating a document, and not an item inside the array. I've tried using arrayUnion but it only adds a new item to the array. I'm trying to update the info[0].tags in the screenshot below. Please let me know if I'm missing information to help make this work.
My issue seems to be I can't do: info[i]: arrayUnion({tags: tags}) so I'm not sure how to tell the updateDoc function which item in the info array I need to update.
Here's the code:
const editCard = async (tags, i) => {
const fieldRef = doc(db, "labels", activeLabels);
await updateDoc(fieldRef, {
info[i]: arrayUnion({ tags: tags }),
});
console.log(tags);
getLabels();
findLabel(activeLabels);
toast.success("Tags Updated!");
};
[![firebase document and array][1]][1]
[1]: https://i.stack.imgur.com/PDJcc.png

Related

firestore updateDoc removes other fields in array [duplicate]

This question already has answers here:
How to update array elements in Firestore with Android?
(2 answers)
How to update an array item in Firestore
(2 answers)
Firestore Update single item in an array field
(2 answers)
Closed 11 months ago.
async function editBackEnd() {
const entryRef = doc(db, "accounts", user.uid)
await updateDoc(entryRef, {
"entry.0.payment": "League"
})
}
I'm trying to update certain fields in an array in firestore. I know there is no direct way to do so but with this code i am able to access the field but it removes all the other entries/fields.
How do I make sure it only updates the field that i want. in this cause im just trying to update payment of index 0
Im having issues uploading pics to stackoverlow so here's a couple url
https://imgur.com/a/1hqNgsn firestore database before updateDoc
https://imgur.com/a/Egzofvz firestore database after updateDoc

How to add field to all documents in database mongoose [duplicate]

This question already has answers here:
Add new field to every document in a MongoDB collection
(7 answers)
Closed 1 year ago.
I am trying to add a field to all documents in the database, but I am not sure how to do that. Here is my database
const db = client.schema
How would I update all documents and add a field called tiks (number) in it?
db.yourCollection.update(
{},
{ $set: {"newField": 1} },
false,
true
)
Parameters
Collection to update since you want all {}
Field to set
Upsert - only add when it is not there hence false
Multi - update multiple documents if matches to query hence true

Removing clicked element from an array [duplicate]

This question already has answers here:
React.js: setState overwriting, not merging
(3 answers)
Closed 2 years ago.
I am trying to have items be addable (done) however I am having an issue with trying to remove an item from the users cart.
Removefromcart = (cart, id) => {
// using an updater function
this.setState(({
// keep everything except the item with the id that was passed in
cart: this.state.cart.filter(cart => cart.id !== id),
// item price - current state total
// total: state.total - price
}));
};
As you can see I am trying to use a filter to remove everything except the id of what was clicked.
However right now it seems to be erasing the array completely. Before that it wasn't affecting the array at all.
In case it is relevant here is a link to my full code: https://codesandbox.io/s/vigilant-dubinsky-0d57h
Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state.
docs
To fix it, use a second form of setState() that accepts a function rather than an object. That function will receive the previous state as the first argument, and the props at the time the update is applied as the second argument:
Blockquote
In your case, you'll need:
this.setState(prevState => ({
cart: prevState.cart.filter(cart => cart.id !== id),
});

How to get specific value or row data from firebase in web [duplicate]

This question already has answers here:
Firebase search by child value
(2 answers)
Closed 4 years ago.
basically I new to firebase, I just want to that how to retrive specific data from firebase ,
[firabase](https://i.stack.imgur.com/vk0S6.png[1]
as you can see in above image I want to retrieve like in Sql we use
Select CellNum from Student where Email="Adnan#gmail.com.
How can I do that in firebase web javascript?
Try the following:
firebase.database().ref().child("Students").orderByChild("Email").equalTo("Adnan#gmail.com").once("value", function (snapshot) {
snapshot.forEach(function(childSnapshot) {
var cellNum=childSnapshot.val().CellNum;
});
});
The snapshot is at Students, then you loop inside the id 22222 and retrieve the CellNum. The orderByChild is the query where Email="Adnan#gmail.com"

How to get the Keys in firebase? [duplicate]

This question already has an answer here:
Firebase retrieve child keys but not values
(1 answer)
Closed 4 years ago.
Hey there i am trying to get the random generated key of datasets from a firebase.
like this:
projects
---12345
---67890
...
i just would like to get the key and safe it to another part in the Database.
I tried that:
but it gives me everything under the node.
getKEY(){
firebase.database().ref('project').once('value', function(snapshot) {
console.log("Key" + JSON.stringify(snapshot.val()) )
});
}
can some one help me out with that? How to get only the Key generated by firebase?
i would like to get these Keys and save it to another dataset but i don't know how to access them?
Try the following:
getKEY(){
firebase.database().ref('project').once('value', function(snapshot) {
snapshot.forEach(function(child) {
var keys=child.key;
});
});
}
more info here:
key property

Categories