Trying to access the adminGetUser function using the AWS CognitoIdentityServiceProvider is giving me some trouble. Although the initialization seems correct, I am encountering the following error message.
[Nest] 43 - 03/29/2022, 9:18:43 AM ERROR [ExceptionHandler] Cannot read properties of undefined (reading 'CognitoIdentityServiceProvider')
TypeError: Cannot read properties of undefined (reading 'CognitoIdentityServiceProvider')
at new UsersService (/usr/src/app/src/users/users.service.ts:21:36)
at Injector.instantiateClass (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:301:19)
at callback (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:48:41)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at Injector.resolveConstructorParams (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:124:24)
at Injector.loadInstance (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:52:9)
at Injector.loadProvider (/usr/src/app/node_modules/@nestjs/core/injector/injector.js:74:9)
at async Promise.all (index 3)
at InstanceLoader.createInstancesOfProviders (/usr/src/app/node_modules/@nestjs/core/injector/instance-loader.js:44:9)
at /usr/src/app/node_modules/@nestjs/core/injector/instance-loader.js:29:13
The relevant code snippet is as follows:
import AWS from 'aws-sdk';
import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { User } from 'models/user.entity';
import { CreateUserDto } from './dto/create-user.dto';
import { InjectRepository } from '@nestjs/typeorm';
@Injectable()
export class UsersService {
config = {
apiVersion: '2016-04-18',
region: 'eu-central-1',
};
cognitoIdentity: AWS.CognitoIdentityServiceProvider;
constructor(
@InjectRepository(User)
private usersRepository: Repository<User>,
) {
this.cognitoIdentity = new AWS.CognitoIdentityServiceProvider(this.config);
}
async findByCognitoSub(cognitoSub: string): Promise<User | undefined> {
const result = await this.usersRepository.find({ where: { cognitoSub } });
if (result.length !== 1) {
return undefined;
}
return result[0];
}
getUser(userPoolId: string, userName: string) {
return this.cognitoIdentity.adminGetUser({
UserPoolId: userPoolId,
Username: userName,
});
}
Any assistance on resolving this issue would be highly appreciated!