“ index.addObject()”时出现阿尔戈利亚搜索错误

时间:2020-04-27 14:53:00

标签: javascript google-cloud-firestore algolia

我已使用以下教程安装了Algolia:https://www.youtube.com/watch?v=dTXzxSlhTDM

我有firestore付费版本,一切正常,直到我在集合中创建了一个项目以尝试它是否正常运行,但是当我这样做时,我的阿尔及利亚索引中添加了任何项目,因此我转到了云函数日志并看到了:

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

1 个答案:

答案 0 :(得分:3)

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

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

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