简体   繁体   中英

Query nested objects firebase

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火力基础架构

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

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

See also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM