Tìm kiếm trang web

typescript - Hàm thêm đối tượng của Algolia không tạo objectId


Tôi đang cố gắng tạo một hàm chạy trên một yêu cầu https để tải lên một tập hợp các tài liệu firestore lên máy chủ algolia để tìm kiếm chỉ mục.

Tôi đã làm điều này thành công với một bộ sưu tập khác, tuy nhiên bộ này không tự động tạo objectId như lần trước.

Tôi đã đọc tài liệu của Algolia và nó khuyến nghị sử dụng

{ autoGenerateObjectIDIfNotExist: true }

Tuy nhiên, cú pháp đó hơi khác so với hàm cloud khác mà tôi đã tạo để tải một bộ sưu tập khác lên Algolia. Tôi đã sử dụng bài viết/hướng dẫn này để viết cả hai hàm. 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)
})

Theo tài liệu, nó nói là phải tải lên một documentId, tuy nhiên mảng tôi dùng để lưu trữ dữ liệu tài liệu có một trường documentId nhưng lại không thể truy cập được.

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

Lỗi :

'objectId' được khai báo nhưng giá trị của nó chưa bao giờ được đọc.

Đây là nhật ký firebase của tôi mà tôi đã sử dụng để xác định rằng có thể là do objectId không được tải lên đúng cách.

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'

Câu trả lời #1

Điều đó trông giống như một cảnh báo lint TypeScript về khai báo objectId của bạn trong đoạn mã này, mà tôi đã định dạng lại để dễ đọc:

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

Bạn khai báo một biến objectId được giải cấu trúc từ giá trị truyền vào then, nhưng bạn không bao giờ sử dụng nó ở bất kỳ đâu trong hàm. Chỉ cần loại bỏ nó nếu bạn không cần sử dụng:

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