【发布时间】:2020-10-17 09:27:21
【问题描述】:
我正在使用 mongoose 为数据库构建一个网站,我目前正在尝试编写一个快速路由,它不会发回存储在给定 ID 处的数据,而是一个响应模式,其中包含每个属性的描述、每个属性的数据类型,以及该属性是否是可选的。
例如,这是我用于存储在我的数据库中的每个产品的模型
const productSchema = new Schema({
name: {type: String, required: true},
description: { type: String, required: false },
image: {type: String, required: false},
expirationDate: { type: Date, required: true },
postedUser: { type: Schema.Types.ObjectId, ref: "User", required: true }
}, {
timestamps: true,
});
这是我为检索给定产品 ID 的数据而编写的路径。
router.route('/').get((req, res, next) => {
Product.find(req.query)
.then(products => {
res.json(normalizer(products));
})
.catch(err => next(err));
});
类似于 url localhost:5550/product/?_id=5ef5521062186e0190da17c9 返回存储在给定 ID 的数据的方式,我想创建一个路由,在调用 url localhost:5550/product 时返回用于产品的模型/schema/?_id=5ef5521062186e0190da17c9.
【问题讨论】:
标签: express mongoose redux mern