簡體   English   中英

使用來自 firebase 的多個 id 獲取數據

[英]fetch data using multiple ids from firebase

我正在做一個迷你電子商務項目,我有一個添加到購物車的功能,所以基本上它是這樣工作的:

我注冊了一個帳戶,每當我將商品添加到購物車時,我都會使用帳戶用戶 ID 將添加到購物車的商品的 ID 存儲在用戶數據中。 像這樣的東西:

/users
  /user-id-here
    ...some data
    ...a collection of ids which are the ids if the items i have bought

/products
  /product-id-1
  ...etc.

問題是,有沒有辦法可以使用我存儲的 id 獲取我購買的產品? 或者有沒有更好的解決這個問題的方法? 因為如果我在“產品”參考中有 15000 件商品,並且如果我獲取所有商品並檢查我的“購買清單”ID 是否包含每個單獨商品的 ID,這是一件非常糟糕的事情......:/

你可以這樣做:

let userProductsIds = ['id-1', 'id-2', '...'];
await db
    .collection(`products`)
    .where(firebase.firestore.FieldPath.documentId(), 'in', userProductsIds);
    .get();

您可以在https://firebase.google.com/docs/firestore/query-data/queries#simple_queries中閱讀有關 Firestore 查詢的更多信息

編輯:

這是你應該做的。

const productsCollection = db.collection('products');
userProductsId.forEach(productId => {
   let product = await productsCollection.doc(`${productId}`).get();
   console.log(product);
})

因此,您只會獲取n次,其中n是用戶的商品購物車列表的大小

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM