sam
sam

Reputation: 969

Loopback v4 nested object(n.o.) without creating that n.o. as separate model

I started with Loopback 4 a week back. And, I have been stuck in some issues for a long time.

I am trying to build a User model

@model()
export class Address {
  @property() addressLine1: string;
  @property() addressLine2: string;
  @property() pin: string;
}

@model()
export class User {
  @property() email: string;
  @property() password: string;
  @property() phone: string;
  @property() address: Address;
}

Issue #1

While I am trying to save the model from the API endpoint, I am getting the following error:

Unhandled error in POST /users: 500 Error: can't resolve reference #/components/schemas/Address from id #

I believe that loopback is trying to save the address as a model. I need that address to be saved as a json field in dynamoDB, instead of creating a new model.

Is there a key/property/setting to passed in the model through which I can ignore that from being created? I went through the tutorials and API docs but found nothing helpful.

Issue #2

Address model shows up in the Swagger only when @model() annotation is added to it

Is there a simpler way to put Address in the swagger without @model?

Note: I am using DynamoDB connector: lb-dynamodb-connector

Upvotes: 0

Views: 442

Answers (1)

Samarpan
Samarpan

Reputation: 943

I think the below should work for you.

@model()
export class Address extends Model {
  @property() addressLine1: string;
  @property() addressLine2: string;
  @property() pin: string;
}

@model()
export class User extends Entity {
  @property() email: string;
  @property() password: string;
  @property() phone: string;
  @property({
    type: 'object',
  }) address: Address;
}

Although I have not worked with dynamo db connector. But that should work as per loopback model goes.

Upvotes: 1

Related Questions