Update of array of objects in a record

i have following record in algolia

enterprise_name:"manaslu enterprise",
objectID:"14417261",quotation:[{shamagri_title:"paper ad",price:4000,
unit:"per sq inc",
description:"5*5",
geo_latitude:40,
geo_longitude:89,
type:"service"}
]

i want to add following object to quotation array while quotation collection gets updated.

const enter =  db.collection("quotation").doc("14417261");

  return enter.update({
    shamagri_title: "banner ad",
    rate: 4000,
    unit: "per sq inch",
    description: "15 * 10",
    type: "service",
  });

i have tried to use following code from cloud function to update algolia record

 functions.firestore
  .document("quotation/{quotationId}")

  .onUpdate((change) => {
    const newData = change.after.data();
    const objectID = change.after.id;
    return index.partialUpdateObjects({
      _quotation: {
        _operation: "Add",
        value: newData,
      },
      objectID,
    });
  });

Based on firebase cloud function it does provide objectId based on update
i have used above partialUpdateObjects function based on following example from algolia

index.partialUpdateObject({
  _tags: {
    _operation: 'AddUnique',
    value: 'public',
  },
  objectID: 'myID',
})
.then(({ objectIDs }) => {
  console.log(objectIDs);
});

however before i even deploy the cloud function, i get following error

 Argument of type '{ _quotation: { _operation: string; value: FirebaseFirestore.DocumentData; }; objectID: string; }' is not assignable to 
parameter of type 'readonly Record<string, any>[]'.
  Object literal may only specify known properties, and '_quotation' does not exist in type 'readonly Record<string, any>[]'.

19       _quotation: {
         ~~~~~~~~~~~~~
20         _operation: "Add",
   ~~~~~~~~~~~~~~~~~~~~~~~~~~
21         value: newData,
   ~~~~~~~~~~~~~~~~~~~~~~~
22       },
   ~~~~~~~

what might have gone wrong here?

Hi @nexus.saugat26,

I see on stackoverflow that you may have solved your issue. Did this work for you?

1 Like