I make a filter that is Man can see women and women can see men that mean user data has ShowMen : true for women user ShowWomen : true for men user
when snapshot from firestore it will return profiles state
const [profiles,setProfiles] = useState([]);
useEffect(() => {
let unsub;
const fetchCards = async()=>{
unsub = onSnapshot(collection(db,'users'),
snapshot=>{
setProfiles(
snapshot.docs
.filter(doc=>
((doc.id!==user.uid)&&(doc.showMen==true&&doc.gender==='male'))
|| ((doc.id!==user.uid)&&(doc.showWomen==true&&doc.gender==='female'))
)
.map(
doc=>({
id :doc.id,
...doc.data()
})
)
)
})//not show already pass/swipe users
}
fetchCards();
return unsub;
}, [])
I cannot filter out that I want doc.id !== user.uid mean authenticated user cannot see his profile view in search list after that if come data is showMen true and gender female, profile array list result out this and display data if come data is showWomen true and gender male , profile array result out this and display data
Finally I want is
if user choose ShowMen option true, she will see only men if user choose ShowWomen option true, he will see only women
How can make this logic in filter() can someone help me I need this thanks
CodePudding user response:
This data filtering should be done on the backend level to avoid downloading unnecessary documents.
To make our life easier, let us redefine our user
data model as below :
{
s_989777: {
age: 25,
displayName: "Roberto Carlos",
gender: "male",
interestedIn: "female",
},
ab9uyrtt: {
age: 21,
displayName: "Christina Liz",
gender: "female",
interestedIn: "male",
},
};
I added new record interestedIn = [gender]
. this value should be equal to one of gender
of existing users. Either male
or female
.
With this new data model, we can use Firestore filtering query.
import { collection, query, where,getDocs } from "firebase/firestore";
const usersRef = collection(db, "users");
// Create a query against the collection.
const interestedIn = currentUser.interestedIn
const q = query(usersRer, where("gender", "==", interestedIn));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
The result will be array of users with gender
user is currently interested in