2

I am trying to get the orders from my db where the email property (which is inside the buyer object) is equal to the user's email; but I keep getting an empty array without orders.

const ProfileContainer = () => {
let { user } = useContext(AppContext)

const [orders, setOrders] = useState([])
const [loading, setLoading] = useState(true)

useEffect(() => {
    console.log(orders)
}, [orders])

useEffect(() => {
    const db = getFirestore()
    const ordersRef = query(collection(db, 'orders'), where(('buyer', 'email'), '==', user.email))
    getDocs(ordersRef)
        .then(res => {
            setOrders(res.docs.map((order) => ({ ...order.data() })))
        })
        .finally(() => setLoading(false))
}, [user])

return (
    <>
        {loading ?
            <Spinner />
            :
            <Profile
                orders={orders}
                user={user}

            />
        }
    </>
)
}

This is my db firebase schema

1 Answer 1

4

Use dot notation to build the path to the nest field.

const ordersRef = query(
    collection(db, 'orders'),
    where('buyer.email', '==', user.email)
)

See also

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer! I tried that, but now this error is showing: "Uncaught Error: Objects are not valid as a React child (found: object with keys {seconds, nanoseconds}). If you meant to render a collection of children, use an array instead."
If you have a new problem, please make a new post to explain what's not working the way you expect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.