Swagger 为什么POST方法对成功插入值的值数组返回500错误?
在loopback 4中,当我发布一个值时,出现了一个奇怪的问题,如:Swagger 为什么POST方法对成功插入值的值数组返回500错误?,swagger,loopbackjs,openapi,loopback4,Swagger,Loopbackjs,Openapi,Loopback4,在loopback 4中,当我发布一个值时,出现了一个奇怪的问题,如: { email: "sit@leoVivamusnibh.net", password: "430975896" } 在swagger中,没有问题报告,但当我发布一系列值时,如: [{ email: "metus.facilisis.lorem@sitamet.org", password: "168978823" }, { email: "fringilla@enim.org", password: "933013104
{
email: "sit@leoVivamusnibh.net",
password: "430975896"
}
在swagger中,没有问题报告,但当我发布一系列值时,如:
[{
email: "metus.facilisis.lorem@sitamet.org",
password: "168978823"
},
{
email: "fringilla@enim.org",
password: "933013104"
}]
该值已过帐到数据库,但出现以下错误:
{
"error": {
"statusCode": 500,
"message": "Internal Server Error"
}
}
在控制台中显示以下行:
POST/users中未处理的错误:500类型错误:model.toObject不是
功能
位于UserRepository.toEntity(D:\apps\test\node_modules@loopback\repository\src\repositories\legacy juggler bridge.ts:471:39)
在UserRepository.create(D:\apps\test\node_modules@loopback\repository\src\repositories\legacy juggler bridge.ts:338:17)
用户模型
@model({ settings: {} })
export class User extends Entity {
@property({
type: 'number',
id: true,
generated: true
})
id: number;
@property({
type: 'string'
})
email: string;
@property({
type: 'string'
})
password: string;
constructor(data?: Partial<User>) {
super(data);
}
}
@model({settings:{}})
导出类用户扩展实体{
@财产({
键入:“编号”,
id:是的,
生成:真
})
id:编号;
@财产({
键入:“字符串”
})
电子邮件:字符串;
@财产({
键入:“字符串”
})
密码:字符串;
构造函数(数据?:部分){
超级(数据);
}
}
用户控制器:
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(User),
},
},
})
user: Omit<User, 'id'>,
): Promise<User> {
return this.userRepository.create(user,
//{ exclude: ['id'] }
);
}
异步创建(
@请求主体({
内容:{
“应用程序/json”:{
模式:getModelSchemaRef(用户),
},
},
})
用户:省略,
):承诺{
返回此.userRepository.create(用户,
//{排除:['id']}
);
}
首先,在环回4中,POST
方法不接受要创建的模型实例数组,它仅适用于单个模型
话虽如此,当请求包含对象数组时,环回不应以500错误响应。我希望得到422个不可处理实体
响应
你能填一只虫子吗 如您所述,您在请求正文中请求的数组很好 问题是如果您想在数据库中创建这些用户配置文件 您必须执行以下操作:
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(User),
},
},
})
userList: User[],
): Promise<User> {
return await Promise.all(
userList.forEach(user => {
return await this.userRepository.create(user);
))
}
异步创建(
@请求主体({
内容:{
“应用程序/json”:{
模式:getModelSchemaRef(用户),
},
},
})
用户列表:用户[],
):承诺{
回报等待承诺(
userList.forEach(用户=>{
return wait this.userRepository.create(user);
))
}
希望这会有所帮助,如果你有任何疑问或建议,请写信给我。
谢谢我已经做了,请检查报告。