如何使用Firestore设计购物车数据?

时间:2018-12-03 12:36:01

标签: reactjs firebase google-cloud-platform google-cloud-firestore

我将Firestore用作后端。我为购物车设计了如下数据:

cart--
     cartid--
           product--
                  id: 'dsdasd',
                  title: 'New Dress',
                  price: 10,
                  images: 'https://someurl/image.jpg
           totalPrice: 50,
           amount: 5

但是我不确定使用它。我想听听有关这种情况的一些建议。我在React.js中构建它。如何使用Firestore设计购物车数据?我应该使用Firebase或Firestore?

1 个答案:

答案 0 :(得分:1)

I'd personally use Firestore, but it also depends on your needs and your app's requirements. You can read more about differences between the Realtime DB and Firestore here.

Talking about your data model, I'm not sure about your actual implementation (it's not clear from the question), but you may need two different collections: products and carts. Every cart document will have a field (array/object) with all the products IDs in that cart. This way you can achieve data consistency, fetching product data from the actual product document every time you need it (for example displaying the cart).

The totalPricefield in the cart is probably unnecessary and leads to data inconsistency, because you need to update it when products in the cart change. It might be better to display the total price in the front end with a simple sum.