Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

91
Views
How to select a single firebase realtime database data using where condition in react?

In my project i want to select a single email address bases on user email using where condition but it is not working. How I can solve this

Database structure in firebase database

facebook-clone-1870b-default-rtdb users
    users
        -MoZJ7ZnwvhQ6I8_M7N8
           active: "9:41:37 PM"
            email:  "dstha221@gmail.com"
            joined_at: "121-101-1"
            name: "J E EV An"
           profile: "https://platform-lookaside.fbsbx.com/platform/p..."

Here is the code

import {useCollection} from "react-firebase-hooks/firestore";
import { db, storage } from "../../firebases";
import {useRouter}  from 'next/router';
import firebase from 'firebase';
const Chats=({id,users,session})=>{
    // const recepiantEmail=GetRecepitentEmail(users,session.user.email);
    useEffect(()=>{
        const data = firebase.database().ref('users').orderByChild(`${id}/email`).equalTo(session.user.name);
        data.once('value',(snapshot)=>{
            console.log("equal address",snapshot.val())
        })
    },[])
console.log(recepitantSnapshot)
    // const recepiant= recepitantSnapshot?.docs?.[0]?.data();
    const router = useRouter();
    const enterChat=()=>{
        // router.push(`/chat/${id}`);
    }
    return(
        <>
        <div className=" flex items-center cursor-pointer p-2">
            <div className="m-1 mr-2 w-3"onClick={enterChat}>
                {/* <img src={recepitantSnapshot.profile}></img> */}
                
            </div>
            {/* <p>{recepitantSnapshot.name}</p> */}
        </div>
        </>
        )
}
export default Chats;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The orderByChild method already finds the property of each child, so you shouldn't include the ${id} in there:

const data = firebase.database().ref('users').orderByChild('email').equalTo(session.user.name);

A second problem: when you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

Your code needs to handle this, by looping of the children of the snapshot with:

data.once('value',(snapshot)=>{
    snapshot.forEach((child) => {
        console.log("equal address",child.key, child.val())
    });
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!