Francesc Torrens
Francesc Torrens

Reputation: 41

Loopback 4 and Mongodb : All model's id are null on response

It is the first time that I use this version (4) for development and I have a problem with loopback and mongodb indexing.

Of the two ids that are inside the db loopback it does not collect any.

It's a problem of the API or DB?


Model [Loopback]

import { Entity, model, property } from '@loopback/repository';

@model()
export class Ad extends Entity {
  @property({
    type: 'number',
    id: true,
    required: true,
  })
  id: number;
 <...>
  constructor(data?: Partial<Ad>) {
    super(data);
  }
}

Data on Mongo:

{
    "_id": {
        "$oid": "5c0e9c7730146d2448746834"
    },
    "id": 110722,
    "creation_date": 1492075600000,
    "update_date": 1492075921000,
    ...
}

Response on loopback GET /ads

[{
    "id": null,
    "creation_date": 1492075600000,
    "update_date": 1492075921000,
    ...
  },...]

Upvotes: 3

Views: 1157

Answers (2)

Arjun Subedi
Arjun Subedi

Reputation: 9

I fixed the same issue by changing the id=String. Mongodb id contains both string and number. So, if you change the type of id=string (Model) the issue will be fixed.

Upvotes: 0

Miroslav Bajtoš
Miroslav Bajtoš

Reputation: 10795

Hello from the LoopBack team :)

I don't see any obvious problem in the code snippets you posted. What happens when you change id's type from number to string? Will it fix the problem?

Most likely, you have found a bug in LoopBack 4. Please report it via GitHub: https://github.com/strongloop/loopback-next/issues

Upvotes: 1

Related Questions