博文原创,且看且珍惜。
需求场景如下图
1、ajax类型为post请求
2、参数在body请求里
3、参数为list 的对象数组并且无参数名 直接就是[{id:1,name:1},{id:2,name:2}] 丢到接口去
4、egg的controller可以通过this.ctx.request.body获取到数组
好的,博主来教你配置他
首先array[string] 肯定是要报错的,因为array力面是object类型
那怎么办
博主翻了下md文档找到了,这么一句话
```js
module.exports = {
Model名称:{
字段名称: { type: "array",required: 字段必要性, itemType:数组元素类型}
}
}
```
type为array,itemType为具体的数组元素类型,支持自定义类型。
对的就是自定义类型,又翻了几个egg-swagger-doc的文档博主找到了解决方案
解决方案
一、/app/contract/database.js(没有目录或js你就新建)代码如下,你要看清楚博主的自定义类型名是 user
"use strict";
module.exports = {
user: {
id: { type: "string", required: true, description: "id 唯一键" },
name: { type: "string", required: true, description: "用户姓名" },
sex: { type: "string", required: true, description: "用户性别" },
age: { type: "integer", required: true, description: "年龄" },
},
};
不用引入,本身egg-swagger就回去读contract下到文件
二、配置swagger的request注释
切入位置如下
/**
* @summary 某某接口
* @Request header string authorization
* @description 测试swagger文档是否可用
* @request body array[user] arr
* @router post /api/xxxxx/create
* @response 200 allString
*/
async create() {
let m=this,{app}=m;
debugger
这里注意 这个user就是前面博主定义的自定义类型
* @request body array[user] arr
好了,原创不易,且行且珍惜。