【问题标题】:Algolia add object function not creating objectIdAlgolia添加对象功能不创建objectId
【发布时间】:2021-02-02 17:35:09
【问题描述】:

我正在尝试创建一个在 https 请求上运行的函数,以将一组 firestore 文档上传到 algolia 服务器以进行索引搜索。

我已经用另一个集合成功地做到了这一点,但是这个并没有像上次那样自动创建 objectId。

我阅读了 algolia 文档,它建议使用

{ autoGenerateObjectIDIfNotExist: true }

但是,该语法与我创建的用于将不同集合上传到 algolia 的其他云函数略有不同。我使用这篇文章/教程来编写这两个函数。 https://medium.com/@soares.rfarias/how-to-set-up-firestore-and-algolia-319fcf2c0d37

.../project/functions/src/index.ts

export const uploadGlobalBucketsToAlgolia = functions.runWith({timeoutSeconds: 400}).https.onRequest(async (req, res) => {
  const algoliaRecords: any[] = []
  const querySnapshot = await db.collection('GlobalBuckets').get()

  querySnapshot.forEach(doc => {
    const document = doc.data()

    const record = {
      objectId : doc.id,
      bucketCreatorId: document.bucketCreatorId,
      bucketCreatorUsername: document.bucketCreatorUsername,
      bucketId: document.bucketId,
      bucketTitle: document.bucketTitle,
      caption: document.caption,
      commentCount: document.commentCount,
      completed: document.completed,
      completedbucketImageURL: document.completedbucketImageURL,
      createdTimestamp: document.createdTimestamp,
      inspirationCount: document.insirationCount,
      likeCount: document.likeCount,
      mostRecentCollectionId: document.mostRecentCollectionId,
      mostRecentCollectionTitle: document.mostRecentCollectionTitle,
      ownerFCMtoken: document.ownerFCMtoken,
      profileImageURL: document.profileImageURL,
      uid: document.uid,
      username: document.username,
      collectionReferences: document.collectionReferences
    }

    algoliaRecords.push(record)
  })

  collectionGlobalBuckets.saveObjects(algoliaRecords, (_error: any, content: any) => {
    res.status(200).send("Global buckets collection was indexed to Algolia successfully.")
  })
  res.send(algoliaRecords)
})

从文档中说要上传 documentId,但是我用来存储文档数据的数组有一个 documentId 字段,但我无法访问它。

collectionGlobalBuckets.saveObjects(algoliaRecords, { autoGenerateObjectIDIfNotExist: true }).then(({ objectId }) => {
    res.status(200).send("Global buckets collection was indexed to Algolia successfully.")
  })

错误:

'objectId' 已声明,但其值从未被读取。

这是我用来确定可能是由于未正确上传 objectId 的 firebase 日志。

2020-10-19T21:27:30.860046917Z D globalBucketsCollectionOnUpdate: Function execution took 188 ms, finished with status: 'ok'
2020-10-19T21:27:33.855935438Z D globalBucketsCollectionOnCreate: Function execution started
2020-10-19T21:27:34.932956286Z D globalBucketsCollectionOnCreate: Function execution took 1079 ms, finished with status: 'ok'
2020-10-19T21:29:18.011004Z I : 
2020-10-19T21:29:18.477917Z I : 
2020-10-19T21:29:19.447697Z N uploadGlobalBucketsToAlgolia: 
2020-10-19T21:30:25.833302Z N uploadGlobalBucketsToAlgolia: 
2020-10-19T21:30:45.414659979Z D uploadGlobalBucketsToAlgolia: Function execution started
2020-10-19T21:30:58.680Z ? uploadGlobalBucketsToAlgolia: Unhandled rejection
2020-10-19T21:30:58.683Z ? uploadGlobalBucketsToAlgolia: { name: 'MissingObjectIDError',
2020-10-19T21:30:58.683Z ? uploadGlobalBucketsToAlgolia:   message:
2020-10-19T21:30:58.683Z ? uploadGlobalBucketsToAlgolia:    'All objects must have an unique objectID (like a primary key) to be valid. Algolia is also able to generate objectIDs automatically but *it\'s not recommended*. To do it, use the `{\'autoGenerateObjectIDIfNotExist\': true}` option.' }
2020-10-19T21:30:58.785616987Z D uploadGlobalBucketsToAlgolia: Function execution took 13371 ms, finished with status: 'connection error'
2020-10-19T21:47:12.846622Z I : 
2020-10-19T21:47:13.195938Z I : 
2020-10-19T21:47:14.127877Z N uploadGlobalBucketsToAlgolia: 
2020-10-19T21:48:32.515157Z N uploadGlobalBucketsToAlgolia: 
2020-10-19T21:48:41.522227723Z D uploadGlobalBucketsToAlgolia: Function execution started
2020-10-19T21:48:56.365Z ? uploadGlobalBucketsToAlgolia: Unhandled rejection
2020-10-19T21:48:56.367Z ? uploadGlobalBucketsToAlgolia: { name: 'MissingObjectIDError',
2020-10-19T21:48:56.367Z ? uploadGlobalBucketsToAlgolia:   message:
2020-10-19T21:48:56.367Z ? uploadGlobalBucketsToAlgolia:    'All objects must have an unique objectID (like a primary key) to be valid. Algolia is also able to generate objectIDs automatically but *it\'s not recommended*. To do it, use the `{\'autoGenerateObjectIDIfNotExist\': true}` option.' }
2020-10-19T21:48:56.471603244Z D uploadGlobalBucketsToAlgolia: Function execution took 14950 ms, finished with status: 'connection error'
2020-10-19T22:00:31.355973248Z D usersCollectionOnUpdate: Function execution started
2020-10-19T22:00:32.943313897Z D usersCollectionOnUpdate: Function execution took 1589 ms, finished with status: 'ok'
2020-10-19T22:06:26.660450Z I : 
2020-10-19T22:06:27.174117Z I : 
2020-10-19T22:06:28.044414Z N uploadGlobalBucketsToAlgolia: 
2020-10-19T22:07:35.324087Z N uploadGlobalBucketsToAlgolia: 
2020-10-19T22:07:56.881712148Z D uploadGlobalBucketsToAlgolia: Function execution started
2020-10-19T22:07:59.266Z ? uploadGlobalBucketsToAlgolia: Unhandled rejection
2020-10-19T22:07:59.269Z ? uploadGlobalBucketsToAlgolia: { name: 'MissingObjectIDError',
2020-10-19T22:07:59.269Z ? uploadGlobalBucketsToAlgolia:   message:
2020-10-19T22:07:59.269Z ? uploadGlobalBucketsToAlgolia:    'All objects must have an unique objectID (like a primary key) to be valid. Algolia is also able to generate objectIDs automatically but *it\'s not recommended*. To do it, use the `{\'autoGenerateObjectIDIfNotExist\': true}` option.' }
2020-10-19T22:07:59.285635593Z D uploadGlobalBucketsToAlgolia: Function execution took 2405 ms, finished with status: 'connection error'

【问题讨论】:

    标签: typescript google-cloud-firestore algolia


    【解决方案1】:

    这看起来像是关于您在此代码中声明 objectId 的 TypeScript lint 警告,为了便于阅读,我已对其进行了格式化:

    collectionGlobalBuckets.saveObjects(
        algoliaRecords,
        { autoGenerateObjectIDIfNotExist: true }
    )
    .then(({ objectId }) => {
        res.status(200).send("Global buckets collection was indexed to Algolia successfully.")
    })
    

    您声明了一个变量objectId,该变量从传递给then 的值中解构,但您从未在函数的任何地方使用它。如果您不需要使用它,只需将其删除:

    .then(() => {
        res.status(200).send("Global buckets collection was indexed to Algolia successfully.")
    })
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 2018-06-01
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      相关资源
      最近更新 更多