I keep returning a Promise {<fulfilled>: {…}}

FirmCiti Inc

Hey guys everytime I try to return some values, i run into this Promise {: {…}}

enter image description here

const [orders, setOrders] = useState({});
    const ordersCollectRef = query(collection(db, "Orders"));
    useEffect(() => {
        const getU = async (id) => {
            // console.log(id, typeof id);
            const docRef = doc(db, "User", id);
            const docSnap = await getDoc(docRef);

            if (docSnap.exists()) {
                return  docSnap.data();
            } else {
                console.log("No such document!");
            }
        };
        const getOrders = async () => {
            const orderData = await getDocs(ordersCollectRef);
            const o = orderData.docs.map((order) => ({
                ...order.data(),
                id: getU(order.data().userId),
            }));
            setOrders(o);
        };
        getOrders();
    }, []);
    console.log(orders);

I am within an async function and using await, so I dont understand.

IndevSmiles

Your getOrders function should look like this:

const getOrders = async () => {
    const orderData = await getDocs(ordersCollectRef);
    const o = await Promise.all(orderData.docs.map(async (order) => ({
        ...order.data(),
        id: await getU(order.data().userId),
    })));
    setOrders(o);
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related