我使用loopback mongodb连接器将loopback4连接到mongodb。
我创建的模型只有两个字段 id 和 name,这两个字段都是必需的。还创建了存储库和 Controller 。但是当我发出 POST 请求时,它给出 422 错误 这是我的模型
import { Entity, model, property } from '@loopback/repository';
@model({ settings: { strict: false } })
export class Ss extends Entity {
@property({
type: 'number',
id: true,
required: true,
})
id: number;
@property({
type: 'string',
required: true,
})
name: string;
[prop: string]: any;
constructor(data?: Partial<Ss>) {
super(data);
}
}
export interface SsRelations {
// describe navigational properties here
}
export type SsWithRelations = Ss & SsRelations;
但是在最初发出 POST 请求时,它只显示名称而不显示 ID。如果我只添加名称,显然会给出 id not specified 错误。为 POST 请求添加 id 后,出现 422 验证失败错误
最佳答案
请检查以下链接,openapi
似乎存在错误,其中 id
被排除在 Controller 架构中
我也遇到了同样的问题,有人指导我。 why Loopback + mongodb is not working
关于node.js - Loopback 4 POST 方法给出 422 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57572320/