Sebas
Sebas

Reputation: 71

Algolia search error when "index.addObject()"

I've installed algolia using this tutorial: https://www.youtube.com/watch?v=dTXzxSlhTDM

I have firestore paid version and all was going okay till i created an item in my collection to try if it was working, but when i did it any item was added in my algolia index, so i went to cloud functions log and saw this:

addToIndex
TypeError: index.addObject is not a function
at exports.addToIndex.functions.firestore.document.onCreate.snapshot (/srv/index.js:15:22)
at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
at /worker/worker.js:825:24
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)

I wasted 30 mins looking at the code, and rewriting it all perfectly exact as in the video, and searched for this error and didn't find anything, so here i'm

index.js

const functions = require('firebase-functions');
const algoliasearch = require('algoliasearch');

const APP_ID = functions.config().algolia.app;
const ADMIN_KEY = functions.config().algolia.key;

const client = algoliasearch(APP_ID, ADMIN_KEY);
const index = client.initIndex('items');

exports.addToIndex = functions.firestore.document('items/{itemId}')
    .onCreate(snapshot => {
        const data = snapshot.data();
        const objectID = snapshot.id;

        return index.addObject({ ...data, objectID });
    });
exports.updateIndex = functions.firestore.document('items/{itemId}')
    .onUpdate((change) => {
        const newData = change.after.data();
        const objectID = change.after.id;
        return index.saveObject({ ...newData, objectID });
    });
exports.deleteFromIndex = functions.firestore.document('items/{itemId}')
    .onDelete(snapshot => index.deleteObject(snapshot.id));

Upvotes: 1

Views: 1593

Answers (1)

Samuel Vaillant
Samuel Vaillant

Reputation: 3857

The addObject method doesn't exist on the latest version. You have to use saveObject:

index.saveObject({ ...data, objectID })

Note that a tutorial for Algolia is available in Firebase documentation.

Upvotes: 6

Related Questions