Home > database >  Filter an array of Objects by value in a firebasedatase in React
Filter an array of Objects by value in a firebasedatase in React

Time:11-21

`i am trying to filter data in a firebase/firestore database table. in fact i have stored user data in a table called users_Infos, the data stored are: name, email and role. i want to filter the role of each user in order to display an appropriate dashboard for them. here documents represent my user list (users_infos) Note that I also have a user that represents the current user and is also an array with a name, email address and uid.

here is the code I'm trying to write that doesn't work.

import { useAuthContext } from '../../hooks/useAuthContext' import { useCollection } from '../../hooks/useCollection' import React from 'react'

//styles import styles from './Home.module.css'

// components import SkillsFormX from './SkillsFormX import SkillsFormY from './SkillsFormY import Footer from '../../components/Footer'

export default function Home() { const { user } = useAuthContext() const { documents, error } = useCollection('users_Infos')

return (

<div className=""> <div className="">

{documents.filter(users => users.displayName === user.displayName).map(filteredPerson => (

  • `{filteredPerson.role==='x' && }` `{filteredPerson.role==='y' && }` `
  • ` `))}`
      `</div>`
      `<Footer />`
    `</div>`
    

    )

    }

    CodePudding user response:

    this don't work

    {documents.filter(users => users.displayName === user.displayName).map(filteredPerson => (

  • {filteredPerson.role}
  • ))}

    CodePudding user response:

    I am sharing with you 2 reference URLs I think will be helpful for you -

    1. https://bobbyhadz.com/blog/react-filter-array-of-objects

    2. https://contactmentor.com/reactjs-filter-array-of-objects/

    • Related