从 firestore 集合中检索数据
2021-04-08
63
我的 Firebase 集合设置如下:
customers
uid
subscriptions
subscriptions uid
items
plan: true or false
我需要访问这个计划变量来检查该计划是活跃的还是不活跃的???如有必要,我可以在 Firestore 中附上配置的照片?但这是我目前用于从 Firestore 获取数据的代码。我是否仍然可以使用它,但可以相应地进行更改?
firestore
.collection("profiledata")
.doc(userID)
.get()
.then((doc) => {
setProfileData(doc.data());
});
在第二张照片中 - 计划部分有一个“行”,显示活动或非活动状态...我需要抓住它!
1个回答
要获取用户的所有订阅(包括项目),应该是:
firebase.collection('customers').doc(userID).collection('subscriptions').get()
.then(subscriptions => {
subscriptions.forEach(subscription => {
// subscription.data() should give you a subscription with its items
console.log(subscription.data());
});
})
如果订阅存在,则查询方式如下,该订阅对于给定客户具有
状态
'active'
:
firebase.collection('customers').doc(userID).collection('subscriptions')
.where('status', '==', 'active').get()
.then(activeSubscriptions => {
// if this is true, the user has no active subscription.
console.log(activeSubscriptions.empty)
});
获取订阅的
状态
:
firebase.collection('customers').doc(userID).collection('subscriptions').get()
.then(subscriptions => {
if (subscriptions.empty) {
console.log('User has no subscriptions')
} else if (subscriptions.size > 1) {
console.log('ERROR: User has multiple subscriptions')
} else {
subscriptions.forEach(theSubscription => {
// there is only this subscription
const data = theSubscription.data();
console.log(data.status);
});
}
});
trixn
2021-04-08