Algolia 添加对象函数不创建 objectId

Algolia add object function not creating objectId

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

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

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

{ autoGenerateObjectIDIfNotExist: true }

但是,该语法与我创建的用于将不同集合上传到 algolia 的其他云函数略有不同。我用这个 article/tutorial 来写这两个函数。 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' is declared but its value is never read.

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

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'

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

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

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

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