2

I am connecting loopback4 to mongodb using loopback mongodb connector.

I created model with only two fields id and name,both required. Repository and controller are also created. But when I make a POST request it is giving 422 error This is my model

    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;

But while making POST request initially it was only showing name and not id. If I add only name obviously it was giving id not specified error. After adding id for POST request it is giving 422 error of validation failed

pratik jaiswal
  • 1,855
  • 9
  • 27
  • 59
  • did you covered how to solve it I am 2 days busy with this error especially in MongoDB datasource. – RSA Sep 01 '19 at 09:50

2 Answers2

1

Please check the following link there it seems there is a bug with openapi which id is excluded in the controller is schema

I had the same problem guys there guide me. why Loopback + mongodb is not working

RSA
  • 1,417
  • 4
  • 22
  • 37
1

You need to take id as string ,not required and generated : true. Then loopback saves Object id as string means if object id is ObjectId("507f1f77bcf86cd799439011") then it saves only '507f1f77bcf86cd799439011' in databse. The another solution would be to take id as number and write code for auto increment of id

pratik jaiswal
  • 1,855
  • 9
  • 27
  • 59