I am trying to make a Flutter and Firebase fitness application that saves runs a user logs. The runs are saved in an array called 'runs' in the Firestore database. Each document in my database represents a different user's runs, so the 'runs' array is a field found within each user's document. I have been trying to delete only a specific child of 'runs' but have had no luck. Does anyone know how to do this?
Here is my code for the delete function:
final CollectionReference runCollection = Firestore.instance.collection('runs')
Future deleteRun(dynamic runToDelete) async {
return await runCollection.document(uid).setData({
'runs': FieldValue.arrayRemove([runToDelete])
});
}
When I run this, I get no errors but nothing happens in the database.
I have also tried FieldValue.delete() but have not been able to isolate a specific index of 'runs' to delete without deleting the entire array.
Here is a picture of my firestore database:
Firestore.instance.collection(collection name).document(your document id).updateData({"array name": FieldValue.arrayRemove([delete object/array])}).than((value){print("delete");}).catchError((error){print(error);});
FieldValue.delete() only works when you know the entire contents of the array item to delete. It does not work with indexes, nor does it work with child values of array items.
What you will have to do instead is read the document, modify the array in memory, then write the modified array back to the document.
Related
I Am using react. I have a fetch setup in an useEffect to grab the data from a database from the backend. It pulls the account and sends it back to the front where i can access it from there.
From the File I want to access the user information i have a console log
I want to be able to grab like the firstname, lastname individually and use them in the script. how do i go about that. I did try to console log UserData.user and it gave me this result
However when i went to try to get firstname like userData.user.firstname i was met with an error. Im pretty new to react and pushing myself with things ive never done before to learn and any help would be great
Thank you everyone who does help it means alot
Please note all information in screenshots are fictitious and are just prop data.
EDIT:
This may be because you are trying to read the user information before the api returns the data. so check if the array has data before trying to access it . You can use save navigation to check for undefined or null
UserData.user[0]?.firstname
Based on your screenshot the data you are getting back is an array of user objects. To access it you will need to use:
userData.user[0].firstname
Note that this will access the first user object in the array - you will likely need checks to ensure the data data exists before accessing it, e.g.:
if (userData.user.length > 0) {
// Access data as needed
}
user is an array so you can acces like this
userData.user[0].firstname;
it will be better if you update your endPoint to get an object instead, so you can have access and check easier
I have a Firebase database with a list of entries. Now, on click, I want to copy a certain document from this database to a user database (create new document in user DB) while at the same time updating one of the fields.
I figured out how to copy the document, but I have problems with updating the field. I think I'm simply using a wrong syntax, but couldn't find this example in Firebase documentation.
This is how the original document looks like: (https://i.stack.imgur.com/ux18x.png)]
And this is how it's copied into the user database: (https://i.stack.imgur.com/QsBVy.png)]
I use this code to make the copy:
setDoc(doc(userSentencesRef, newSentenceIDText), ...newSentence)
And I tried many ways to update the "Level" field, but none of them works:
setDoc(doc(userSentencesRef, newSentenceIDText), ...newSentence, {data: {LEVEL: "exclude"}})
setDoc(doc(userSentencesRef, newSentenceIDText), ...newSentence, {LEVEL: "exclude"})
setDoc(doc(userSentencesRef, newSentenceIDText), {...newSentence, LEVEL: "exclude"})
(the first two do nothing, the last one adds a separate level field).
I also wouldn't want to list every single field that needs to be added { EN: data.EN, DE: data.DE }, etc, because I want to make sure it works even if the other fields change in future.
I will really appreciate your help.
I am using Reactjs and Firestore.
I have this collection of products:
The colorMap is a map then below it are the different colors and their quanty.
Now, I want to create a list or a history whenever a product is added and whenever the quantity in those colors was added more of it.
Should I add another collection that will store when a product is added or whenever quantities are added in the color? I'm also thinking of adding a createdDate
Or there any other way I could do this? As much as possible, I won't be using any cloud functions.
A common way to keep the history of each document is by creating a subcollection under that document (say history) and writing a new document with either the complete, old document data there for every update you perform, or a new document with just the old values of the fields that were modified.
While it is convenient to do this from Cloud Functions, as they already get both the previous and the new data for each document write, you can accomplish the same from client-side code too.
I have the following hierarchy on firebase, some data are hidden for confidentiality:
I'm trying to get a list of videos IDs (underlines in red)
I only can get all nodes, then detect their names and store them in an array!
But this causes low performance; because the dataSnapshot from firebase is very big in my case, so I want to avoid retrieving all the nodes' content then loop over them to get IDs, I need to just retrieve the IDs only, i.e. without their nested elements.
Here's my code:
new Firebase("https://PRIVATE_NAME.firebaseio.com/videos/").once(
'value',
function(dataSnapshot){
// dataSnapshot now contains all the videos ids, lines & links
// this causes many performance issues
// Then I need to loop over all elements to extract ids !
var videoIdIndex = 0;
var videoIds = new Array();
dataSnapshot.forEach(
function(childSnapshot) {
videoIds[videoIdIndex++] = childSnapshot.name();
}
);
}
);
How may I retrieve only IDs to avoid lot of data transfer and to avoid looping over retrived data to get IDs ? is there a way to just retrive these IDs directly ?
UPDATE: There is now a shallow command in the REST API that will fetch just the keys for a path. This has not been added to the SDKs yet.
In Firebase, you can't obtain a list of node names without retrieving the data underneath. Not yet anyways. The performance problems can be addressed with normalization.
Essentially, your goal is to split data into consumable chunks. Store your list of video keys, possible with a couple meta fields like title, etc, in one path, and store the bulk content somewhere else. For example:
/video_meta/id/link, title, ...
/video_lines/id/...
To learn more about denormalizing, check out this article: https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html
It is a bit old, and you probably already know, but in case someone else comes along. You can do this using REST api call, you only need to set the parameter shallow=true
here is the documentation
So, I have a collection called users where I store the user data like password, username and so on, and then I have another collection called couples where I store inside an array of the users who are in that couple.
In the angular page I want to check if the current user logged in has a couple, so I search for its ID in the couples collection like this:
$scope.couples = Couples.query({
members: $scope.authentication.user._id
});
This return the couple info if the user ID is found in a couple in the couples collection.
The problem is that I get in my scope an array and I can't figure it out how to access the data inside that array (inside the array is the object with the couple information).
So in the $scope.couple if I print the value I get everything in console, but $scope.couple[0].members fails or $scope.couple.members fails.
If a go to the network usage I see a call to couples that returns the info in a json, but I can't understand why is an array in my scope.
Thanks