0

I am trying to push data from MongoDB to Algolia using Redux, and it IS importing data. However, it is not importing data into individual array, but rather the whole object.

Here's what I mean: algolia index

How would I extrapolate each individual array?

  const passwordList = useSelector((state) => state.passwordList);
  const { loading, error, passwords } = passwordList;

  useEffect(() => {
    dispatch(listPasswords());
  }, [dispatch]);

  const objects = [{ passwords }];

  index
    .saveObjects(objects, { autoGenerateObjectIDIfNotExist: true })
    .then(({ objectIDs }) => {
      console.log(objectIDs);
    });
Toan Lam
  • 109
  • 2
  • 12
  • Not sure what you mean by "individual array" I only see one array of password objects. why do you pack the array inside another array here `const objects = [{ passwords }];` that will yield `[[{}, {}, {}]]`? – Bugbeeb Jan 17 '22 at 01:55

1 Answers1

1

saveObjects takes in an array of objects
const objects = [{ passwords }]; will create a new array with only 1 object that's why it shows only 1 record.

[{
  objectID: 1234 //created from autoGenerateObjectIDIfNotExist: true
  passwords: [{ ... }, { ... }]
}]

Since your passwords is already an array of password objects you can directly pass it to the saveObjects and it will create individual record for each element in the array

  index
    .saveObjects(passwords, { autoGenerateObjectIDIfNotExist: true })
    .then(({ objectIDs }) => {
      console.log(objectIDs);
    });

PS. it is recommended to have objectID defined instead of auto generating. I have come across issues where records get duplicated when auto generated object IDs are used when indexing large number of records at a time.
Also it is not recommended to index sensitive information.

cmgchess
  • 7,996
  • 37
  • 44
  • 62
  • i managed to use useEffect to fetch with the API with axios. it's in the database with each element in the array. however, the Hits component is not rendering even though I have data in the index. But, SearchBox is rendering. Also, instead of using .savedObjects I used .replaceAllObjects. I wonder if it would efffect the search speed when replacing all data though. – Toan Lam Jan 19 '22 at 05:55
  • is your data in your index have 1 record for each passwords object – cmgchess Jan 19 '22 at 06:29
  • Yes there is one record for each password object – Toan Lam Jan 19 '22 at 13:59
  • Nvm just misspelled the index name. Oops – Toan Lam Jan 19 '22 at 17:47
  • how would I properly define an object id if I want to use saveObjects and prevent duplication? – Toan Lam Jan 26 '22 at 06:19
  • You can run a for loop on your password array and add a objectID field to each. You can probably have your _id value as objectID value and remove the _id field from array – cmgchess Jan 26 '22 at 06:42
  • could you give me an example? – Toan Lam Jan 26 '22 at 21:22
  • https://stackoverflow.com/questions/6809659/changing-the-key-name-in-an-array-of-objects – cmgchess Jan 27 '22 at 03:53