Seb*_*bas 1 javascript algolia google-cloud-firestore
我已经使用本教程安装了 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)
Run Code Online (Sandbox Code Playgroud)
我浪费了 30 分钟查看代码,并按照视频中的方式完全准确地重写了代码,并搜索了此错误但没有找到任何内容,所以我在这里
索引.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));
Run Code Online (Sandbox Code Playgroud)
该addObject方法在最新版本中不存在。你必须使用saveObject:
index.saveObject({ ...data, objectID })
Run Code Online (Sandbox Code Playgroud)
请注意,Firebase文档中提供了 Algolia 教程。
| 归档时间: |
|
| 查看次数: |
702 次 |
| 最近记录: |