Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用可选参数创建端点?(节点、Express、MongoDB)_Node.js_Mongodb_Express_Api Design_Optional Parameters - Fatal编程技术网

Node.js 如何使用可选参数创建端点?(节点、Express、MongoDB)

Node.js 如何使用可选参数创建端点?(节点、Express、MongoDB),node.js,mongodb,express,api-design,optional-parameters,Node.js,Mongodb,Express,Api Design,Optional Parameters,我已经创建了一个MongoDB,它包含两个主要实体猫和品种(一对多),但我从未使用过可选参数 我想处理以下请求 获取所有猫的“GET”请求(可选参数将 确定是否也应返回描述,以及另一个可选选项 参数(按品种过滤) 获取特定cat的GET请求(带有可选参数 这将确定有效负载是否也将通过 姓名或身份证 我包括我的数据库模式,以备需要 const CatSchema = new Schema({ name: { type: String, required: true, trim: t

我已经创建了一个MongoDB,它包含两个主要实体猫和品种(一对多),但我从未使用过可选参数

我想处理以下请求

  • 获取所有猫的“GET”请求(可选参数将 确定是否也应返回描述,以及另一个可选选项 参数(按品种过滤)

  • 获取特定cat的GET请求(带有可选参数 这将确定有效负载是否也将通过 姓名或身份证

我包括我的数据库模式,以备需要

const CatSchema = new Schema({
    name: { type: String, required: true, trim: true },
    description: { type: String, required: true, trim: true },
    breed: {
        type: Schema.Types.ObjectId,
        ref: "Breed"
    }
});

const BreedSchema = new Schema({
    name: { type: String, required: true, trim: true }
});

使用问号将参数设置为可选。
/cats/:描述

还可以使用多个可选参数。
/cats/:描述?/:品种

编辑, 确保使用以下工具检查参数:

if(description) {
    //show cats with description
} 
差不多吧