javascript - “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));
解决方案
该addObject
方法在最新版本中不存在。你必须使用saveObject
:
index.saveObject({ ...data, objectID })
请注意,Firebase文档中提供了 Algolia 教程。
推荐阅读
- intellij-idea - gradlew clean deployNodes - QUASAR 警告:Quasar Java 代理未运行
- soap - 错误:在 Sabre 发送 CreatePassengerNameRecordRS 请求时出现 ERR.SP.BUSINESS_ERROR
- c++ - 什么是 msvc_x64_x64 以及与 msvc_x64 有何不同
- c# - 如何在 C# 中隐藏打印弹出窗口?
- python - 损失在 Pytorch 中不会收敛,但在 TensorFlow 中会收敛
- javascript - 为什么打印此变量会显示提示,而不是错误消息?
- git - 试图从功能分支中挑选:没有“樱桃”?
- sql - 使用 HQL 计算两个字段的差异之间的范围
- nginx - nginx 从 x-forwarded 获取源客户端 ip 以使用 map 模块
- java - 无法在杰克逊 xml 中序列化 OffsetDateTime