0

[I want to update specific index of firestore database via javascript i tried alot but there is no way for me :(

Data View of my products

    const ref = firestore
    .collection('collections')
    .doc("dsflkjeowjjljsd3Xfkl")
    .items.doc(productIndex);

  ref
    .update(
      {
        [`items.${productIndex}`]: fieldUnion({...objectToUpdate, id: productId}),
      },
      {merge: true}
    ) // sets the contents of the doc using the id
    .then(() => {
      // fetch the doc again and show its data
      ref.get().then((doc) => {
        console.log(doc.data()); // prints {id: "the unique id"}
      });
    });
Jan Hernandez
  • 4,414
  • 2
  • 12
  • 18
Junaid
  • 21
  • 2
  • Please add a code snippet showing what you've tried and the error(s) you are getting – Ali Nauman Mar 05 '21 at 15:50
  • Thanks please check now – Junaid Mar 05 '21 at 16:36
  • When you want to show a screen-grab, please paste the image *here*. Following a link is both dangerous and inconvenient to *us*. The easier you make it for us to see your issues, the more likely you are to get answers. – LeadDreamer Mar 05 '21 at 16:42
  • Sorry but the image uploader i got from stackoverflow so i am unable to show direct images i tried but now working – Junaid Mar 05 '21 at 16:48
  • There is no way to update a single item in an array with the Firestore API. You will need to update the array in its entirety, typically by: 1) reading the document, 2) getting the entire array from it, 3) updating the item in the array, 4) writing the entire updated array back to the database. – Frank van Puffelen Mar 05 '21 at 17:55

0 Answers0