每次从 stripe.checkout.sessions.listLineItems 返回待处理的承诺

A pending promise is returned from stripe.checkout.sessions.listLineItems each time

我在这里尝试从 firebase 获取数据,然后使用该 ID 从条带结帐中检索项目。 但是每次我尝试这个我都会得到一个未决的承诺。

const colRef = collection(db, `users/${session.user.email}/orders`);
const q = query(colRef, orderBy("timestamp", "desc"));

const orders = await getDocs(q)
    .then((snapshot) => {
      snapshot.docs.forEach((sdoc) => {
        orders.push({
          id: sdoc.id,
          items: stripe.checkout.sessions
            .listLineItems(sdoc.id, {
              limit: 100,
            })
            .then((res) => {
              return res;
            })
            .catch((err) => console.log(err)),
        });
      });
      return orders;
    })
    .catch((err) => console.log(err));

我也尝试过等待,但我的整个数组都返回了空

const colRef = collection(db, `users/${session.user.email}/orders`);
  const q = query(colRef, orderBy("timestamp", "desc"));

  orders = await getDocs(q)
    .then((snapshot) => {
      snapshot.docs.forEach(async (sdoc) => {
        orders.push({
          id: sdoc.id,
          items: await stripe.checkout.sessions
            .listLineItems(sdoc.id, {
              limit: 100,
            })
            .then((res) => {
              return res;
            })
            .catch((err) => console.log(err)),
        });
      });
      return orders;
    })
    .catch((err) => console.log(err));

您需要将承诺集合与 Promise.all 结合起来。它看起来像这样:

const snapshot = await getDocs(q);
const orders = await Promise.all(snapshot.docs.map(async (sdoc) => {
  const sessions = await stripe.checkout.sessions
            .listLineItems(sdoc.id);
  return {id: sdoc.id, items: sessions};
}));