I would like to filter my search term against all the data from the databse.
I think the logic would be: capitalize both searchterm and data, then compare searchterm with each field from the data. Then setData() to that filtered search.
The const Data is used in a flatlist so my filtered data will be shown here.
Thank you for any assistance.
My code:
const SearchFeed = (searchterm) => {
const q = query(PostsCollectionRef, orderBy('timestamp', 'desc'));
onSnapshot(q, querySnapshot => {
const id = querySnapshot.docs.map(doc =>
({
id: doc.id,
timestamp: doc.data().timestamp,
postID: doc.data().PostID,
title: doc.data().status + " " + doc.data().Type,
status: doc.data().status,
location: doc.data().location,
type: doc.data().Type,
injured: doc.data().Injured,
collar: doc.data().Collar,
color: doc.data().Colour,
username: doc.data().username[0],
description: doc.data().Description,
imagesrc: doc.data().picture
}))
//need to capitalize searchterm
//need to capitalize filter value
//eg id.filter((capitalize(val) => val.type == capitalize(searchterm)
const Animal = id.filter((val) => val.type == searchterm)
setData(Animal)
console.log(Animal)
});
}
Since you have all the posts data on client already, you can use a package like Lunr to add full text search functionality in web app. You can use add() method to add data of all documents fetched and then search() to look for documents instead of using a filter().
Do note that you are fetching all the documents from Posts collection every time so that involves lots of read charges. You can algo try using Algolia (with Firebase Algolia Extension). This indexes all the documents added to Firestore in Algolia and then you can directly query data from Algolia based on the search term.