首页 > 解决方案 > 如何使用 Angular/Ionic 和 Stripe 扩展查询 firebase firestore 子集合以获取其他属性

问题描述

我正在将 Angular/Ionic 与 Firestore 和 Stripe 的 Firebase Extension for Subscriptions 一起使用。我已将产品添加到 Stripe 的后端,然后使用产品集合和定价子集合填充 Firestore。每种产品都有多种价格可供选择。我正在尝试查询产品和定价,以便在结帐页面上显示可供购买的产品。我可以获得产品,但我无法获得每种产品的价格。每种产品的定价差异是基于时间的(单个、每月、每年)。以下是 Stripe 如何在https://github.com/stripe/stripe-firebase-extensions/blob/master/firestore-stripe-subscriptions/POSTINSTALL.md的 github 页面上描述条带扩展的过程 列出可用的产品和价格产品和定价信息是 Cloud Firestore 中的正常集合和文档,可以这样查询:

db.collection('${param:PRODUCTS_COLLECTION}')
  .where('active', '==', true)
  .get()
  .then(function (querySnapshot) {
    querySnapshot.forEach(async function (doc) {
      console.log(doc.id, ' => ', doc.data());
      const priceSnap = await doc.ref.collection('prices').get();
      priceSnap.docs.forEach((doc) => {
        console.log(doc.id, ' => ', doc.data());
      });
    });
  });

这是我尝试过的:

const prodRef = this.afs
   .collection<Product>('products', (ref) => ref.where('active', '==', true));

this.products = prodRef.valueChanges();

this.prices = await prodRef.snapshotChanges().pipe(
   map((actions) => {
     actions.map((a) => {
       const data = a.payload.doc.data() as Product;
       data.id = a.payload.doc.id;
       return data;
     });
   })
 );
 // this.pricing = await prodRef.valueChanges().toPromise().then((querySnapshot) => {
 //   querySnapshot.forEach((doc) => {
 //     console.log(doc.id, ' => ', doc.data());
 //     const priceSnap = snapshotChanges collection('prices').get();
 //     priceSnap.docs.forEach((doc) => doc.data())
 //   })
 // })
 
 // snapshotChanges().pipe(map(actions => {
 //      actions.map(a => {
 //       const data = a.payload.doc.data();
 //       const id = a.payload.doc.id;
 //       return { id, ...data };
 //     });
 //   }));

标签: angulargoogle-cloud-firestoregoogle-cloud-functionsstripe-payments

解决方案


推荐阅读