user2828442
user2828442

Reputation: 2515

Firestore function saving entire document data to Algolia

I have created a Function which is saving the Entire Document to Algolia when onCreate function is triggered. I want to save only three fields and not the entire document.

Here is my current function code:

exports.addToIndex = functions.firestore.document('questions/{questionsId}')
        .onCreate((snapshot: { data: () => any; id: any; desc:any; }) => {
            const data = snapshot.data();
          //  const descdata = snapshot.data().desc;
            const objectID = snapshot.id;
            console.log(objectID);
         //   console.log(descdata);
         console.log(data);
    
          //  return index.saveObject({ ...descdata, objectID });
          return index.saveObject({ ...data, objectID });
        });

I only want to save three objectid's:

  1. ObjectID
  2. slatex
  3. alatex

At present, it is saving all 18 fields of the document. How can I do this?

Upvotes: 0

Views: 76

Answers (1)

Muthu Thavamani
Muthu Thavamani

Reputation: 1405

The data may carry the entire firestore document.

For limited fields, I usually frame the index data separately; like below

exports.addToIndex = functions.firestore.document('questions/{questionsId}')
        .onCreate((snapshot) => {
            const data = snapshot.data();
            const objectID = snapshot.id;
            let _index_data = {
                 'objectID': objectID,
                 'slatex': data.slatex,
                 'alatex': data. alatex
            }
    
            return index.saveObject(_index_data);
 });

Upvotes: 2

Related Questions