【发布时间】:2019-07-28 21:45:16
【问题描述】:
我建立了一个包含 3 门课程的课程页面,
我想以一种所有课程都显示有购买按钮的方式构建它,课程信息是通过products 集合从数据库中收集的。
那么我希望如果用户购买了课程,而不是购买,将显示手表。
为此,我有 2 个数组: 1)所有产品 2) 用户购买的产品
现在我想比较它们,并从第一个数组中删除用户已经购买的所有产品。
我尝试在网上查方法,但我完全不明白。
这是获取数组的函数:
const productRef = await db.collection("products").get();
const products = await productRef.docs.map(doc => {
return {
id: doc.id,
name: doc.data().name,
price: doc.data().price
};
});
console.log(products);
//[ { id: 'course1', name: 'Course 1', price: 25 },
{ id: 'course2', name: 'Course 2', price: 10 },
{ id: 'course3', name: 'Course 3', price: 30 } ]
if (user) {
const claims = res.locals.decodedClaims;
const uid = claims.uid;
const email = claims.email;
const userProductsRef = await db
.collection("users")
.doc(uid)
.collection("courses")
.get();
if (userProductsRef.docs.length > 0) {
const userProducts = userProductsRef.docs.map(doc => {
return { id: doc.id, name: doc.data().name };
});
console.log(userProducts);//[ { id: 'course1', name: 'Course 1' } ]
///COMPARE HERE the two arrays
}
}
现在我想通过 id 将产品与 userProducts 进行比较。
所以最后的预期结果应该是这样的:
products= [
{ id: 'course2', name: 'Course 2', price: 10 },
{ id: 'course3', name: 'Course 3', price: 30 } ];
userProducts= [ { id: 'course1', name: 'Course 1' } ]
谢谢!
【问题讨论】:
标签: javascript arrays node.js firebase