node.js - Loopback 4 POST 方法给出 422 错误

标签 node.js mongodb typescript loopback4

我使用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/

相关文章:

javascript - Jest Node.js 无法使用单独的文件正确调用 beforeAll 和 afterAll

node.js - Mongoose:如何从express.js应用程序中的上限集合中获取数据?

mongodb - 如何通过mongodb中的两个键删除重复条目?

typescript - 如何在 TypeScript 中声明仅包含对象而不包含函数的类型

typescript - typescript 中的可选属性类

node.js - 如何获得与 nodeJS 一起使用的基本 Shopify GraphQL Admin API?

javascript - 中止 doc.save() 内的 mongooseJS 保存过程

arrays - Meteor、MongoDB通过订阅获取数组的一部分

javascript - 未处理的 promise 拒绝 [错误 : Got an invalid value for 'component' prop for the screen 'LocationType' . 它必须是有效的 React 组件。]

node.js - MongoDB 填充性能