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;
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())
});
})