如何使用 Firebase-Cloud-Functions 更新我的 Algolia 索引

发布于 2025-01-16 12:45:41 字数 1110 浏览 2 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2025-01-23 12:45:41

该错误与 Algolia 无关。它是由 TypeScript 抛出的,因为您尝试在 onUpdate firestore 触发器中访问 snap.id

查看 API 参考 (https://firebase.google。 com/docs/reference/functions/providers_firestore.documentbuilder.html#onupdate)您可以看到传递给回调函数的第一个参数是类型更改。 Change 使用通用类型(在本例中为 DocumentQuerySnapshot)作为其 beforeafter 属性。 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 the onUpdate 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 case DocumentQuerySnapshot) for its before and after properties. The Change type itself does not have a property id.

Therefore try accessing it like so: snap.after.id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文