"index.addObject()" 时 Algolia 搜索错误

Algolia search error when "index.addObject()"

我已经使用本教程安装了 algolia:https://www.youtube.com/watch?v=dTXzxSlhTDM

我有 firestore 付费版本,一切正常,直到我在我的 collection 中创建了一个项目来尝试它是否有效,但是当我这样做时,任何项目都被添加到我的 algolia 索引中,所以我转到云功能日志并看到了这个:

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)

我花了 30 分钟查看代码,并完全按照视频中的方式重写了代码,并搜索了这个错误但没有找到任何东西,所以我在这里

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));

addObject 方法在最新版本中不存在。你必须使用 saveObject:

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

请注意,Firebase 中提供了 Algolia 教程 documentation