如何使用 Firebase-Cloud-Functions 更新我的 Algolia 索引
我使用 algolia 作为我的 firebase 应用程序的全文搜索,我能够通过 firebase-cloud-functions 创建和删除索引,但要更新我已经尝试过但没有成功,这里是功能:
//* am using Typescript
const env = functions.config()
const client = algoliasearch(env.algolia.app_id, env.algolia.admin_api_key)
const index = client.initIndex('Products')
//the function to create index
export const onProductCreated = functions.firestore
.document('Products/{productId}')
.onCreate((snap, ctx) => {
return index.saveObject({
objectID: snap.id,
...snap.data(),
})
})
//the function to delete index
export const onProductDeleted = functions.firestore
.document('Products/{productId}')
.onDelete((snap, ctx) => {
return index.deleteObject(snap.id)
})
然后更新它我尝试了这个函数
export const onProductCreated = functions.firestore
.document('Products/{productsId}')
.onUpdate((snap, ctx) => {
return index.partialUpdateObject({
objectID: snap.id,
...snap.data(),
})
})
但它不起作用,当我部署函数“属性‘id’在类型‘Change
Am Using algolia as a full-text search for my firebase app and I was able to create and delete indexes via firebase-cloud-functions but to updated I've tried and did not succeed here are the functions:
//* am using Typescript
const env = functions.config()
const client = algoliasearch(env.algolia.app_id, env.algolia.admin_api_key)
const index = client.initIndex('Products')
//the function to create index
export const onProductCreated = functions.firestore
.document('Products/{productId}')
.onCreate((snap, ctx) => {
return index.saveObject({
objectID: snap.id,
...snap.data(),
})
})
//the function to delete index
export const onProductDeleted = functions.firestore
.document('Products/{productId}')
.onDelete((snap, ctx) => {
return index.deleteObject(snap.id)
})
then to update it I tried this functions
export const onProductCreated = functions.firestore
.document('Products/{productsId}')
.onUpdate((snap, ctx) => {
return index.partialUpdateObject({
objectID: snap.id,
...snap.data(),
})
})
But it didn't work, It gave an this error when I deployed the functions "Property 'id' does not exist on type 'Change< QueryDocumentSnapshot >'."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误与 Algolia 无关。它是由 TypeScript 抛出的,因为您尝试在
onUpdate
firestore 触发器中访问snap.id
。查看 API 参考 (https://firebase.google。 com/docs/reference/functions/providers_firestore.documentbuilder.html#onupdate)您可以看到传递给回调函数的第一个参数是类型
更改
。 Change 使用通用类型(在本例中为DocumentQuerySnapshot
)作为其before
和after
属性。Change
类型本身没有属性id
。因此,尝试像这样访问它:
snap.after.id
。The error is not related to Algolia. It is thrown by TypeScript because you are trying to access
snap.id
in theonUpdate
firestore trigger.Looking at the API reference (https://firebase.google.com/docs/reference/functions/providers_firestore.documentbuilder.html#onupdate) you can see that the first argument passed to the callback function is of type
Change<DocumentQuerySnapshot>
. Change uses the generic type (in this caseDocumentQuerySnapshot
) for itsbefore
andafter
properties. TheChange
type itself does not have a propertyid
.Therefore try accessing it like so:
snap.after.id
.