【问题标题】:How do I send a response schema in a MERN Stack?如何在 MERN 堆栈中发送响应模式?
【发布时间】: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


    【解决方案1】:

    您可以在模型上使用 .schema 属性来访问其架构

    router.route('/').get((req, res, next) => {
       console.log(Product.schema)
    
       res.json(Product.schema)
     });
    
    

    我不确定是.schema 还是.Schema,你必须测试一下

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 2022-08-07
      • 1970-01-01
      相关资源
      最近更新 更多