Struggling with AWS CognitoIdentityServiceProvider issues

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!

Answer №1

It's a mystery to me how this solution ended up working. Initially, I was importing AWS using

import AWS from 'aws-sdk';

However, after some trial and error, I found success by changing my import statement to

import * as AWS from 'aws-sdk';

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The NPM package manager is unable to locate jquery.js and jquery.keyframes.js

I've encountered an issue while working with jquery and jquery.keyframes. My project structure involves storing NPM js files in the node_modules folder and html files in the public folder. However, when I use the following code, the html file is unabl ...

What is the best way to access a JSON object that has dashes in its key, returned from TranslateService?

Utilizing TranslateService from @ngx-translate/core to retrieve strings in the current language can be done as shown below: this.translate .get(['hotline-card.email-subject', 'hotline-card.email-body']) .subscribe((strings) => { ...

Convert epoch time to HHMM format using JavaScript

I have a specific variable that stores epoch time data. My goal is to showcase the time information in HHMM format. Below you can find the code I am currently using; function convertEpochTimeToDateObj(epoch_time) { var utcSeconds = epoch_time; va ...

How can I use jQuery to switch the positions of two <div> elements in HTML based on the selection of a dropdown value?

I need to switch the display positions of two <div> containers based on a dropdown value change. Can this be accomplished using jQuery? Here are the two div containers whose display positions need to be interchanged: <div id="first"><p> ...

pre-iframe loading function

Is it possible to capture the state of an iframe while data is loading? The onload event only fires once all content has finished loading. I would appreciate any assistance with this issue. Thank you. ...

Troubleshooting template routing issues in Angular UI

Below is the content of my router.js: angular.module('app.routes', []).config(function($stateProvider, $urlRouterProvider) { $stateProvider.state('tabsController.dashboard', { url : '/page2', views : { ' ...

Is there a way for me to implement a "view more posts" button on

I need help figuring out how to hide the "Show More" button when there are no posts. I have created a showLoad function and an isLoad state variable, but I'm unsure of how to implement this. The button display logic is dependent on the isLoad state. ...

Working with relative paths in React Native TypeScript using WebStorm

My variable color is located in the path app/theme. To set it up, I created a package.json file in app/package.json with the following content: { "name": "app" } Now, to import color in TypeScript files, I use the following syntax: import { color } fro ...

Tips for enhancing the color saturations of cells that overlap in an HTML table

My goal is to enhance the color of overlapping cells in my HTML tables. For instance, when I click on cell 2, the .nextAll(':lt(5)') method will change the class of the next 4 cells. https://i.stack.imgur.com/mwv8x.png Next, clicking on cell 3 ...

Adjust the hue of a circle based on whether the user's position falls within its designated range

I am working with a map that displays several circles, each with its own radius. When the page loads, I capture the user's position and show it on the map. Initially, all circles are red. My goal is to determine if the user's current position fal ...

Invert the motion within a photo carousel

Is there anyone who can assist me with creating a unique photo slider using HTML, CSS, and JS? Currently, it includes various elements such as navigation arrows, dots, and an autoplay function. The timer is reset whenever the arrows or dots are clicked. Ev ...

The reason Typescript is able to accurately determine the value when providing a Generics that extends specific types

Exploring Different Generics in TypeScript: When using generics in TypeScript, the type of data you receive can vary based on how you define your functions. // Example with string generic type function getResult<T>(...v: T[]) { return v } const s ...

Retrieve the id of an element and a standard element using JavaScript

I have a CSS selector #menu li {background-color: red;}. I'm trying to retrieve its properties using JavaScript. It's crucial for me to access both the #menu and li elements separately as they have distinct attributes. Unfortunately, methods lik ...

Generating a menu in one-hour intervals

Could someone please advise on the best approach to generate a markup like the one below using Angular: <select> <option value="0000">00:00AM</option> <option value="0100">01:00AM</option> <option value="0200" ...

Ways to Deduct 10 Days from a Date using JavaScript

After receiving some helpful suggestions, I managed to solve my date issue by using moment.js. Here is the snippet of code where I implemented moment.js: var preorderdate = moment(date).subtract('days',10).format('MMMM D, YYYY'); var r ...

value assigned to ng-model does not update beyond its scope

My issue involves binding an ng-model to an input field. Despite this, the value of the variable it is connected to does not update outside of the specific div where the directive is defined: <div input-field ng-if="startTypes.selected.value == &a ...

Achieve this unique effect with titanium scroll view elements

I recently came across a fascinating feature in some applications. The feature involves an image with the letter A positioned in a specific area. As I scroll up, the label containing the letter A moves upward until it reaches the top of the screen. At tha ...

Implement a CSS transition when switching between images

On my website, I have a feature that switches between a down arrow and an up arrow. You can view it here. I would like to implement a CSS transition so that there is a brief pause between the switch. How can I achieve this? Below is the code snippet: ...

What are some ways to access Angular's form array within HTML code?

.ts import { Component, OnInit } from '@angular/core'; import { HelloServiceService } from 'src/app/hello-service.service'; import { FormBuilder, FormGroup, FormControl, FormArray, Validators } from '@angular/forms'; @Compone ...

I am interested in extracting information from the Firebase real-time database and showcasing it on my HTML webpage

I am struggling to display data from the Firebase real-time database on my HTML page. Even though I can see all the data perfectly in the console, it doesn't show up on the webpage. I attempted to use a for loop, but it still doesn't display the ...