node.js - 如果产品已添加到使用 express.js 和 mongodb 制作的产品目录中的愿望清单/购物车中,如何获取信息
问题描述
在我的产品目录中,如果产品已经在我的愿望清单/购物车中,我该如何分配信息?我想为已经在愿望清单/购物车中的产品禁用“添加到愿望清单”。
我将 express.js 与猫鼬一起使用。目前,我将愿望清单存储在用户对象中,并引用产品。
const userSchema = new Schema({
email: { type: String, required: true },
password: { type: String, required: true },
wishlist: {
items: [
{
ProductId: { type: Schema.Types.ObjectId, ref: 'Product', required: true }
}
]
}
});
我使用 pug 查询产品目录Product.find({})
并渲染结果。
解决方案
(更新)
尝试这样的事情:
控制器/Store.js
const Users = require("../models/Users")
const Products = require("../models/Products")
router.post("/addToWishlist", async(req, res) =>{
try{
//query for specified user
const user = await Users.findOne({
email: req.body.email,
password: req.body.password
});
//query for all products except for what's on the list
let stuff = await Products.find(
{_id:{ $not: {$in:[...user.wishlist.items]}}
});
res.status(200).json({ // or res.send/res.render
data: stuff
})
});
}
catch(err){
res.status(500).json({ // or res.send/res.render
message: err.message
})
}
});
推荐阅读
- r - R:在数据框中的特定位置插入行
- cross-compiling - 使用 RISC-V 工具链交叉编译 V8
- mysql - MYSQL查询以获取每行具有不同子项的数组列表
- vba - 为什么 Powerpoint VBA 中不存在 .MergeShape 方法
- bash - 找不到源 $(pipenv --venv)/bin/activate 的源
- python - 检查通道之间的像素值组合
- c# - EF - 在不重新循环的情况下分配相关实体的 ID
- sql-server - SQL Server:根据文本字段中的 csv 值按用户分组的聚合计数
- amazon-ec2 - 试图将 Ansible 剧本从我的本地机器复制到远程主机。但是获得了错误的权限
- visual-studio - 我们可以自定义 Visual Studio 的颜色主题(不是 VS Code,不是编辑器颜色)吗?