0

Im using Google Firestore with Angular and I'm trying to update a specific element inside an array.

I've tried a lot of things but with no succsess at all.

How can I reach to the specific company index inside the companies array?

Thanks :)

enter image description here

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Nauruto
  • 143
  • 1
  • 12
  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve)" – Frank van Puffelen Nov 15 '18 at 14:09

1 Answers1

2

How can I reach to the specific company index inside the companies array?

There is no way you can get, update or remove an element in a Firestore array according with the corresponding index. Try to think what might happen if a user wants to edit a value at index 0, some other user wants to delete the value at index 0 and in the same time another user wants to add another value at index 1, you'll end up having very different results and why not, an ArrayIndexOutOfBoundsException. So Firestore actions with arrays are a little bit different. So you cannot perform actions like, insert, update or delete at a specific index. So use arrays only if don't care about the exact order that you store elements.

Firestore added a few weeks ago some features to add or remove specific elements but only if don't care about the exact position of them. See here official documentation.

In your case, you don't have a simple array of strings you have an array of objects, which means that in order to do some chnages, you should get the entire object, iterate over the properties, make the necessary changes and write it back to the database.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Ok. Actually I dont care about the exact position. I just want to edit an elemet and write it back to the array in the database. Any ideas of how to do it? – Nauruto Nov 15 '18 at 13:30
  • If you don't care about the position, as you initial have asked then you just should make your own attempt given the information in the answer and in the offical documentation regarding [adding data](https://firebase.google.com/docs/firestore/manage-data/add-data) and ask another question if something else comes up. – Alex Mamo Nov 15 '18 at 13:42
  • I edited my question my friend. For now I manage to do it by deleting the specific object from the array and insert the updated one after. Is there any other way to do it? – Nauruto Nov 15 '18 at 14:00
  • 1
    I rolled back your question. Don't edit your actual question to add another one. Let's stick to one question at a time. If you have another problem after this one is explained, post a new question for that problem with its own MCVE. – Alex Mamo Nov 15 '18 at 14:06
  • So in order to follow the rules of this comunity, please post another fresh question, so me and other users can help you. – Alex Mamo Nov 15 '18 at 14:06