codepip55
codepip55

Reputation: 53

TypeError: Cannot read properties of undefined (reading 'findOne') NestJS Mongoose Schema

I have an API made in NestJS but one of my services seems to have issues accessing my mongoose model. I've checked over everything numerous times and couldnt find anything missing, and the models are imported the same in other services and they work find. You can find the code repository here: https://github.com/codepip55/pip-api/tree/dev. It's gonna be under the dev branch and the files giving the error (see below) are under src/oauth2.

Error:

TypeError: Cannot read properties of undefined (reading 'findOne')
    at Oauth2Service.getClient (C:\Users\**********\Documents\Coding\Javascript\nestjs\pip-api\src\oauth2\oauth2.service.ts:38:46)

Console.logging the model gives undefined as well.

The error orgiginates from:

 async getClient(
    clientId: string,
    clientSecret?: string,
  ): Promise<{
    client: {
      id: string;
      redirectUris: string[];
      grants: string[];
    };
  }> {
    console.log('model', await this.oauthClientModel)
    try {
      let client;
      if (!clientSecret)
        client = await this.oauthClientModel.findOne({ clientId });
      else
        client = await this.oauthClientModel.findOne({
          clientId,
          clientSecret,
        });
      if (!client) throw new NotFoundException();

      return {
        client: {
          id: client.clientId,
          redirectUris: client.redirectUris,
          grants: client.clientGrants,
        },
      };
    } catch (err) {
      console.log(1, err);
    }
  }

Thanks for any help!

Upvotes: 1

Views: 410

Answers (1)

Hailemariam Addisu
Hailemariam Addisu

Reputation: 16

I Think you miss oauth cliend model.you should import it.

Upvotes: 0

Related Questions