1
  • Loopback 4
  • MongoDB v4.2.3

I'm teaching myself Loopback 4 and I have a problem in handling id. Even though I have my model id type set to string it reports it's of type number in /explorer. My goal is to use mongodb generated ids but this is blocking my progress.

My model:

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

@model({
  settings: {
    strictObjectIDCoercion: true,
  },
})
export class SearchInputs extends Entity {
  @property({
    type: 'string',
    id: true,
  })
  id?: string;

...

In explorer I can fetch all the documents in the database but I can not fetch any document by id

enter image description here

I've read all the documentation and various tutorials and this is never mentioned. Does any one know why I have this problem?

chris loughnane
  • 2,648
  • 4
  • 33
  • 54

1 Answers1

1

When I first created the model it was of type number and when I manually changed it to string I only partially updated its controller. You must update the @param type too.

async findById(
    @param.path.string('id') id: string,
    @param.filter(SearchInputs, {exclude: 'where'})
    filter?: FilterExcludingWhere<SearchInputs>,
  ): Promise<SearchInputs> {
    return this.searchinputsRepository.findById(id, filter);
  }

It now works as expected.

chris loughnane
  • 2,648
  • 4
  • 33
  • 54