Import data from API to Algolia
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:
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);
});
saveObjects
takes in an array of objectsconst 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.