首页 > 解决方案 > “index.addObject()”时的 Algolia 搜索错误

问题描述

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

我有 Firestore 付费版本,一切正常,直到我在我的收藏中创建了一个项目以尝试它是否正常工作,但是当我这样做时,任何项目都添加到了我的 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));

标签: javascriptgoogle-cloud-firestorealgolia

解决方案


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

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

请注意,Firebase文档中提供了 Algolia 教程。


推荐阅读