Search code examples
loopbackjsstrongloop

How do you find an item in a repository by something other than id


If I have a repository with many properties and I want to find something by the non-id property, do I just find all and then return the data after a boolean comparison, or is there a better way to find by a property that's not the ID?


Solution

  • In loopback4, you need to use repository for this purpose. Do as below.

    For case where you know there will be just one entry with value. (Unique columns)

    const user = await this.userRepository.findOne({
      where: {
        username: 'test_admin'
      }
    });
    

    For case where there can be multiple.

    const user = await this.userRepository.find({
      where: {
        firstName: 'test admin'
      }
    });