1

I'm new to web development, so I'm extremely sorry if the question is rife with inaccuracies.

My firestore database is composed like so:

enter image description here

Is there a way in which I could update bookname under a specific card (for instance card1)

Right now my approach is to use the following function:

enter image description here

But when I execute the IssueBook() function, I get an error shown on the console:

enter image description here

As far as I can see, the card1 field does have two fields within which can be updated so what does the console message imply?

Thank you in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Deb
  • 355
  • 2
  • 9
  • It's not possible. Have a look at this answer[see here](https://stackoverflow.com/a/46773121/13030940) – Mahesh Jul 19 '22 at 10:22
  • 1
    Going forward, please don't post screenshots of your code, or other textual content. Instead post the actual text, and use the formatting tools of Stack Overflow to mark it up. Also see: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) – Frank van Puffelen Jul 19 '22 at 14:21
  • 1
    @AlexMamo: it looks like OP is trying to update a nested **map** field here, not an array, so I'm going to remove the duplicate and reopen the question. – Frank van Puffelen Jul 19 '22 at 14:22
  • @FrankvanPuffelen Oh, yes, you're right. Sorry, my bad. – Alex Mamo Jul 19 '22 at 14:29
  • @Mahesh OP want to update ***map*** field not an array. And by the way of array you linked and answer where was an update that says it's now possible to update an array. To update array field, use `arrayUnion()` function or `arrayRemove()` to remove item and update array. – Mises Jul 19 '22 at 14:34
  • @Mises while you can nowadays atomically add a new, unique item to an array (with `array-union`) and remove an item from it (with `arrayRemove`), there's still no way to **update** an existing item in an array without reading the entire array into your code, updating it there, and then writing the entire array back to the database again. – Frank van Puffelen Jul 19 '22 at 15:14
  • @FrankvanPuffelen Yeah technically it's not an update. Because I append/push a new item, not update. – Mises Jul 19 '22 at 15:27
  • @FrankvanPuffelen About `arrayUnion()` a snippet in documentation inside a library says it works with `@firebase/firestore/lite#(setDoc)` ? This is a mistake in documentation ? Because `arrayRemove()` got nothing about `@firebase/firestore/lite` so it should work in both libraries. – Mises Jul 19 '22 at 15:43
  • I'm not sure where that is Mises. But if you think there's a mistake in the docs please file feedback on the relevant page/repo, or open a separate question with a link and quotes and I'll have a look. – Frank van Puffelen Jul 19 '22 at 16:09
  • @FrankvanPuffelen I created a question on StackOverflow: https://stackoverflow.com/questions/73043715/firestore-arrayunion-arrayremove-links-inside-documentation-not-works – Mises Jul 19 '22 at 21:37

2 Answers2

4

It looks like you have a nested field under LibCard. To update a field in a nested object you can use dot notation:

updateDoc(doc(db, "users", docid), {
    "LibCard.card1.bookname": "Coraline"
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

In my case category is an array this must me exact name of existing nested object in a document.

const userData = doc(db, "users", userUid);
            await updateDoc(userData, {
              category,
            });

Console.log category array on React Native enter image description here

Firebase document. enter image description here